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 | 7f08d12b0b02f02dcb48b459baa0cab5b5f62113 (patch) | |
tree | 4c9b4d0c34c940757caee284fdbe39e47ee85ec2 | |
parent | aedc347b292f60d3216c11eb509c6067fba782f9 (diff) | |
download | bcm5719-llvm-7f08d12b0b02f02dcb48b459baa0cab5b5f62113.tar.gz bcm5719-llvm-7f08d12b0b02f02dcb48b459baa0cab5b5f62113.zip |
[profile] Further cleanup/refactor file handling code
Also get rid of the redundant strncpy calls.
llvm-svn: 270730
-rw-r--r-- | compiler-rt/lib/profile/InstrProfilingFile.c | 60 |
1 files changed, 26 insertions, 34 deletions
diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c index 69c03253ce0..856d8f7e600 100644 --- a/compiler-rt/lib/profile/InstrProfilingFile.c +++ b/compiler-rt/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 |