operf_stats.cpp

Go to the documentation of this file.
00001 
00013 #include <stdio.h>
00014 #include <sys/types.h>
00015 #include <sys/stat.h>
00016 #include <fcntl.h>
00017 
00018 #include "operf_stats.h"
00019 #include "op_get_time.h"
00020 
00021 unsigned long operf_stats[OPERF_MAX_STATS];
00022 
00026 using namespace std;
00027 void operf_print_stats(string sessiondir, char * starttime, bool throttled)
00028 {
00029     string operf_log (sessiondir);
00030     int total_lost_samples = 0;
00031 
00032     operf_log.append("/samples/operf.log");
00033     FILE * fp = fopen(operf_log.c_str(), "a");
00034     if (!fp) {
00035         fprintf(stderr, "Unable to open %s file.\n", operf_log.c_str());
00036         return;
00037     }
00038     fprintf(fp, "\nProfiling started at %s", starttime);
00039     fprintf(fp, "Profiling stopped at %s", op_get_time());
00040     fprintf(fp, "\n-- OProfile/operf Statistics --\n");
00041     fprintf(fp, "Nr. non-backtrace samples: %lu\n", operf_stats[OPERF_SAMPLES]);
00042     fprintf(fp, "Nr. kernel samples: %lu\n", operf_stats[OPERF_KERNEL]);
00043     fprintf(fp, "Nr. user space samples: %lu\n", operf_stats[OPERF_PROCESS]);
00044     fprintf(fp, "Nr. samples lost due to sample address not in expected range for domain: %lu\n",
00045            operf_stats[OPERF_INVALID_CTX]);
00046     fprintf(fp, "Nr. lost kernel samples: %lu\n", operf_stats[OPERF_LOST_KERNEL]);
00047     fprintf(fp, "Nr. samples lost due to sample file open failure: %lu\n",
00048         operf_stats[OPERF_LOST_SAMPLEFILE]);
00049     fprintf(fp, "Nr. samples lost due to no permanent mapping: %lu\n",
00050         operf_stats[OPERF_LOST_NO_MAPPING]);
00051     fprintf(fp, "Nr. user context kernel samples lost due to no app info available: %lu\n",
00052            operf_stats[OPERF_NO_APP_KERNEL_SAMPLE]);
00053     fprintf(fp, "Nr. user samples lost due to no app info available: %lu\n",
00054            operf_stats[OPERF_NO_APP_USER_SAMPLE]);
00055     fprintf(fp, "Nr. backtraces skipped due to no file mapping: %lu\n",
00056            operf_stats[OPERF_BT_LOST_NO_MAPPING]);
00057     fprintf(fp, "Nr. hypervisor samples dropped due to address out-of-range: %lu\n",
00058            operf_stats[OPERF_LOST_INVALID_HYPERV_ADDR]);
00059     fprintf(fp, "Nr. samples lost reported by perf_events kernel: %lu\n",
00060            operf_stats[OPERF_RECORD_LOST_SAMPLE]);
00061 
00062     if (operf_stats[OPERF_RECORD_LOST_SAMPLE]) {
00063         fprintf(stderr, "\n\n * * * ATTENTION: The kernel lost %lu samples. * * *\n",
00064                 operf_stats[OPERF_RECORD_LOST_SAMPLE]);
00065         fprintf(stderr, "Decrease the sampling rate to eliminate (or reduce) lost samples.\n");
00066     } else if (throttled) {
00067         fprintf(stderr, "* * * * WARNING: Profiling rate was throttled back by the kernel * * * *\n");
00068         fprintf(stderr, "The number of samples actually recorded is less than expected, but is\n");
00069         fprintf(stderr, "probably still statistically valid.  Decreasing the sampling rate is the\n");
00070         fprintf(stderr, "best option if you want to avoid throttling.\n");
00071     }
00072 
00073     // TODO: handle extended stats
00074     //operf_ext_print_stats();
00075 
00076     for (int i = OPERF_INDEX_OF_FIRST_LOST_STAT; i < OPERF_MAX_STATS; i++)
00077         total_lost_samples += operf_stats[i];
00078 
00079     if (total_lost_samples > (int)(OPERF_WARN_LOST_SAMPLES_THRESHOLD
00080                        * operf_stats[OPERF_SAMPLES]))
00081         fprintf(stderr, "\nSee the %s file for statistics about lost samples.\n", operf_log.c_str());
00082 
00083     fflush(fp);
00084     fclose(fp);
00085 }

Generated on 8 Nov 2012 for Oprofile by  doxygen 1.6.1