From: Philippe E. <ph...@us...> - 2001-11-30 23:38:33
|
Update of /cvsroot/oprofile/oprofile/dae In directory usw-pr-cvs1:/tmp/cvs-serv7566/oprofile/dae Modified Files: opd_proc.c opd_util.c opd_util.h oprofiled.c Log Message: op_time: a new utility to see ratio of samples for each samples files Index: opd_proc.c =================================================================== RCS file: /cvsroot/oprofile/oprofile/dae/opd_proc.c,v retrieving revision 1.79 retrieving revision 1.80 diff -u -d -r1.79 -r1.80 --- opd_proc.c 2001/11/14 21:19:01 1.79 +++ opd_proc.c 2001/11/30 23:37:59 1.80 @@ -66,7 +66,7 @@ static void opd_put_mapping(struct opd_proc *proc, int image_nr, u32 start, u32 offset, u32 end); static struct opd_proc *opd_get_proc(u16 pid); static void opd_delete_proc(struct opd_proc *proc); -static void opd_handle_old_sample_file(int counter, const char * mangled, time_t mtime); +static void opd_handle_old_sample_file(const char * mangled, time_t mtime); static void opd_handle_old_sample_files(const char * mangled, time_t mtime); /* every so many minutes, clean up old procs, msync mmaps, and @@ -179,39 +179,16 @@ } /** - * opd_save_old_sample_file - back up the sample file - * @file: the file name of the sample - * - * Back up a sample file. - */ -static void opd_save_old_sample_file(const char *file) -{ - char * savename; - int gen = 0; - - savename = opd_malloc(strlen(file) + 1 + 10); - strcpy(savename, file); - do { - sprintf(savename + strlen(file), "-%d", gen++); - } while (opd_get_fsize(savename, 0)); - - printf("Renaming old sample file as \"%s\"\n", savename); - - if (rename(file, savename)) - perror("Couldn't rename : "); - - opd_free(savename); -} - -/** * opd_handle_old_sample_file - deal with old sample file * @mangled: the sample file name * @mtime: the new mtime of the binary * * If an old sample file exists, verify it is usable. - * If not, move or delete it. + * If not, move or delete it. Note than at startup the daemon + * check than the last (session) events settings match the + * currents */ -static void opd_handle_old_sample_file(int counter, const char * mangled, time_t mtime) +static void opd_handle_old_sample_file(const char * mangled, time_t mtime) { struct opd_header oldheader; FILE * fp; @@ -226,16 +203,8 @@ if (memcmp(&oldheader.magic, OPD_MAGIC, sizeof(oldheader.magic)) || oldheader.version != OPD_VERSION) goto closedel; - /* versions match, but we might be using different values */ - if (difftime(mtime, oldheader.mtime) || - oldheader.ctr_event != ctr_event[counter] || - oldheader.ctr_um != ctr_um[counter] || - oldheader.ctr_count != ctr_count[counter] || - oldheader.cpu_type != cpu_type) { - fclose(fp); - opd_save_old_sample_file(mangled); - return; - } + if (difftime(mtime, oldheader.mtime)) + goto closedel; fclose(fp); verbprintf("Re-using old sample file \"%s\".\n", mangled); @@ -272,7 +241,7 @@ for (i = 0 ; i < op_nr_counters ; ++i) { sprintf(mangled + len, "#%d", i); - opd_handle_old_sample_file(i, mangled, mtime); + opd_handle_old_sample_file(mangled, mtime); } free(mangled); Index: opd_util.c =================================================================== RCS file: /cvsroot/oprofile/oprofile/dae/opd_util.c,v retrieving revision 1.24 retrieving revision 1.25 diff -u -d -r1.24 -r1.25 --- opd_util.c 2001/11/13 21:21:01 1.24 +++ opd_util.c 2001/11/30 23:37:59 1.25 @@ -789,6 +789,42 @@ return count; } +/** + * opd_move_regular_file - move file between directory + * @new_dir: the destination directory + * @old_dir: the source directory + * + * move the file @old_dir/@name to @new_dir/@name iff + * @old_dir/@name is a regular file + * + * if renaming succeed zero or the file is not + * a regular file is returned + */ +int opd_move_regular_file(const char *new_dir, + const char *old_dir, const char *name) +{ + int ret = 0; + struct stat stat_buf; + + char * src = opd_malloc(strlen(old_dir) + strlen(name) + 2); + char * dest = opd_malloc(strlen(new_dir) + strlen(name) + 2); + + strcpy(src, old_dir); + strcat(src, "/"); + strcat(src, name); + + strcpy(dest, new_dir); + strcat(dest, "/"); + strcat(dest, name); + + if (!stat(src, &stat_buf) && S_ISREG(stat_buf.st_mode)) + ret = rename(src, dest); + + opd_free(src); + opd_free(dest); + + return ret; +} /** * opd_poptGetContext - wrapper for popt Index: opd_util.h =================================================================== RCS file: /cvsroot/oprofile/oprofile/dae/opd_util.h,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- opd_util.h 2001/11/13 21:21:01 1.18 +++ opd_util.h 2001/11/30 23:38:00 1.19 @@ -120,6 +120,9 @@ char *opd_get_time(void); char *opd_get_line(FILE *fp); +int opd_move_regular_file(const char *new_dir, const char *old_dir, + const char *name); + #ifdef __cplusplus } #endif Index: oprofiled.c =================================================================== RCS file: /cvsroot/oprofile/oprofile/dae/oprofiled.c,v retrieving revision 1.49 retrieving revision 1.50 diff -u -d -r1.49 -r1.50 --- oprofiled.c 2001/11/14 21:19:01 1.49 +++ oprofiled.c 2001/11/30 23:38:00 1.50 @@ -21,6 +21,7 @@ extern u32 ctr_count[OP_MAX_COUNTERS]; extern u8 ctr_event[OP_MAX_COUNTERS]; extern u8 ctr_um[OP_MAX_COUNTERS]; +static u32 ctr_enabled[OP_MAX_COUNTERS]; uint op_nr_counters = 2; static int showvers; @@ -154,6 +155,133 @@ } /** + * opd_backup_samples_files - back up all the samples file + * + * move all files in dir @smpdir to directory + * @smdir/session-#nr + */ +static void opd_backup_samples_files(void) +{ + char * dir_name; + int gen = 0; + struct stat stat_buf; + DIR *dir; + struct dirent *dirent; + + dir_name = opd_malloc(strlen(smpdir) + strlen("session-") + 10); + strcpy(dir_name, smpdir); + + do { + sprintf(dir_name + strlen(smpdir), "/session-%d", ++gen); + } while (stat(dir_name, &stat_buf) == 0); + + if (mkdir(dir_name, 0755)) { + /* That's a severe problem: if we continue we can overwrite + * samples files and produce wrong result. FIXME */ + printf("unable to create directory %s\n", dir_name); + + exit(1); + } + + if (!(dir = opendir(smpdir))) { + printf("unable to open directory %s\n", smpdir); + + exit(1); + } + + printf("Backing up samples file to directory %s\n", dir_name); + + while ((dirent = readdir(dir)) != 0) { + if (opd_move_regular_file(dir_name, smpdir, dirent->d_name)) { + printf("unable to backup %s/%s to directory %s\n", + smpdir, dirent->d_name, dir_name); + } + } + + closedir(dir); + + opd_free(dir_name); +} + +/** + * opd_need_backup_samples_files - test if we need to + * backup samples files + * + * We can't backup lazilly samples files else it can + * leads to detect than backup is needed after some + * samples has been written (e.g. ctr 1 have the same + * setting from the previous runs, ctr 0 have different + * setting and the first samples output come from ctr1) + * + */ +static int opd_need_backup_samples_files(void) +{ + DIR * dir; + struct dirent * dirent; + struct stat stat_buf; + int need_backup; + /* bitmaps: bit i is on if counter i is enabled */ + int counter_set, old_counter_set; + uint i; + + if (!(dir = opendir(smpdir))) { + printf("unable to open directory %s\n", smpdir); + + exit(1); + } + + counter_set = old_counter_set = 0; + need_backup = 0; + + while ((dirent = readdir(dir)) != 0 && need_backup == 0) { + char * file = opd_malloc(strlen(smpdir) + strlen(dirent->d_name) + 2); + strcpy(file, smpdir); + strcat(file, "/"); + strcat(file, dirent->d_name); + if (!stat(file, &stat_buf) && S_ISREG(stat_buf.st_mode)) { + struct opd_header header; + FILE * fp = fopen(file, "r"); + if (!fp) + continue; + + if (fread(&header, sizeof( header), 1, fp) != 1) + goto close; + + if (memcmp(&header.magic, OPD_MAGIC, sizeof(header.magic)) || header.version != OPD_VERSION) + goto close; + + if (header.ctr_event != ctr_event[header.ctr] || + header.ctr_um != ctr_um[header.ctr] || + header.ctr_count != ctr_count[header.ctr] || + header.cpu_type != cpu_type) { + need_backup = 1; + } + + old_counter_set |= 1 << header.ctr; + + close: + fclose(fp); + } + + opd_free(file); + } + + for (i = 0 ; i < op_nr_counters; ++i) { + if (ctr_enabled[i]) + counter_set |= 1 << i; + } + + /* old_counter_set == 0 means there is no samples file in the sample + * dir, so avoid to try to backup else whe get empty backup dir */ + if (old_counter_set && old_counter_set != counter_set) + need_backup = 1; + + closedir(dir); + + return need_backup; +} + +/** * opd_options - parse command line options * @argc: argc * @argv: argv array @@ -213,6 +341,9 @@ sprintf(filename, "/proc/sys/dev/oprofile/%d/unit_mask", i); ctr_um[i]= opd_read_int_from_file(filename); + + sprintf(filename, "/proc/sys/dev/oprofile/%d/enabled", i); + ctr_enabled[i]= opd_read_int_from_file(filename); } for (i = 0 ; i < op_nr_counters ; ++i) { @@ -433,6 +564,10 @@ opd_init_images(); opd_go_daemon(); + + if (opd_need_backup_samples_files()) { + opd_backup_samples_files(); + } opd_open_files(); |