diff options
author | Xinliang David Li <davidxl@google.com> | 2016-05-25 17:30:15 +0000 |
---|---|---|
committer | Xinliang David Li <davidxl@google.com> | 2016-05-25 17:30:15 +0000 |
commit | 4502401cf8de82c0e2a12aa8fe6971b0d6e56121 (patch) | |
tree | 6f42620e6361c8a431c262b969e6779daf346693 /lib/profile/InstrProfilingFile.c | |
parent | 1ae308d63305cf4ac26285f2eebdda54d046928d (diff) |
[profile] Further cleanup/refactor file handling code
Also get rid of the redundant strncpy calls.
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@270730 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/profile/InstrProfilingFile.c')
-rw-r--r-- | lib/profile/InstrProfilingFile.c | 60 |
1 files changed, 26 insertions, 34 deletions
diff --git a/lib/profile/InstrProfilingFile.c b/lib/profile/InstrProfilingFile.c index 69c03253c..856d8f7e6 100644 --- a/lib/profile/InstrProfilingFile.c +++ b/lib/profile/InstrProfilingFile.c @@ -110,41 +110,20 @@ static void truncateCurrentFile(void) { fclose(File); } -/* Set the result of the file name parsing. If \p FilenamePat pattern is seen - * the first time, also truncate the file associated with that name. - */ -static void setFilename(const char *FilenamePat, const char *PidStr, - unsigned NumPids, const char *HostStr, - unsigned NumHosts) { - /* Check if this is a new filename and therefore needs truncation. */ - int NewFile = - !lprofCurFilename.FilenamePat || - (FilenamePat && strcmp(FilenamePat, lprofCurFilename.FilenamePat)); - - lprofCurFilename.FilenamePat = FilenamePat; - lprofCurFilename.NumPids = NumPids; - if (NumPids) - strncpy(lprofCurFilename.PidChars, PidStr, MAX_PID_SIZE); - lprofCurFilename.NumHosts = NumHosts; - if (NumHosts) - strncpy(lprofCurFilename.Hostname, HostStr, COMPILER_RT_MAX_HOSTLEN); - - /* If not a new file, append to support profiling multiple shared objects. */ - if (NewFile) - truncateCurrentFile(); -} - static void resetFilenameToDefault(void) { - setFilename("default.profraw", 0, 0, 0, 0); + memset(&lprofCurFilename, 0, sizeof(lprofCurFilename)); + lprofCurFilename.FilenamePat = "default.profraw"; } /* Parses the pattern string \p FilenamePat and store the result to * lprofcurFilename structure. */ + static int parseFilenamePattern(const char *FilenamePat) { int NumPids = 0, NumHosts = 0, I; - char PidChars[MAX_PID_SIZE]; - char Hostname[COMPILER_RT_MAX_HOSTLEN]; + char *PidChars = &lprofCurFilename.PidChars[0]; + char *Hostname = &lprofCurFilename.Hostname[0]; + lprofCurFilename.FilenamePat = FilenamePat; /* Check the filename for "%p", which indicates a pid-substitution. */ for (I = 0; FilenamePat[I]; ++I) if (FilenamePat[I] == '%') { @@ -168,10 +147,25 @@ static int parseFilenamePattern(const char *FilenamePat) { } } - setFilename(FilenamePat, PidChars, NumPids, Hostname, NumHosts); + lprofCurFilename.NumPids = NumPids; + lprofCurFilename.NumHosts = NumHosts; return 0; } +static void parseAndSetFilename(const char *FilenamePat) { + int NewFile; + const char *OldFilenamePat = lprofCurFilename.FilenamePat; + + if (!FilenamePat || parseFilenamePattern(FilenamePat)) + resetFilenameToDefault(); + + NewFile = + !OldFilenamePat || (strcmp(OldFilenamePat, lprofCurFilename.FilenamePat)); + + if (NewFile) + truncateCurrentFile(); +} + /* Return buffer length that is required to store the current profile * filename with PID and hostname substitutions. */ static int getCurFilenameLength() { @@ -242,8 +236,7 @@ void __llvm_profile_initialize_file(void) { /* Detect the filename and truncate. */ FilenamePat = getFilenamePatFromEnv(); - if (!FilenamePat || parseFilenamePattern(FilenamePat)) - resetFilenameToDefault(); + parseAndSetFilename(FilenamePat); } /* This API is directly called by the user application code. It has the @@ -252,8 +245,7 @@ void __llvm_profile_initialize_file(void) { */ COMPILER_RT_VISIBILITY void __llvm_profile_set_filename(const char *FilenamePat) { - if (!FilenamePat || parseFilenamePattern(FilenamePat)) - resetFilenameToDefault(); + parseAndSetFilename(FilenamePat); } /* @@ -268,8 +260,8 @@ void __llvm_profile_override_default_filename(const char *FilenamePat) { const char *Env_Filename = getFilenamePatFromEnv(); if (Env_Filename) return; - if (!FilenamePat || parseFilenamePattern(FilenamePat)) - resetFilenameToDefault(); + + parseAndSetFilename(FilenamePat); } /* The public API for writing profile data into the file with name |