root/examples/perfcounter/perf_writer.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_constants
  2. output_num_instances
  3. output_perf_desc
  4. initialize
  5. refresh_perf_data_block
  6. output_perf_counter
  7. output_perf_instance
  8. output_global_data
  9. output_perf_data_block
  10. update_counters
  11. main

   1 /* 
   2  *  Unix SMB/CIFS implementation.
   3  *  Performance Counter Daemon
   4  *
   5  *  Copyright (C) Marcin Krzysztof Porwit    2005
   6  *  
   7  *  This program is free software; you can redistribute it and/or modify
   8  *  it under the terms of the GNU General Public License as published by
   9  *  the Free Software Foundation; either version 3 of the License, or
  10  *  (at your option) any later version.
  11  *  
  12  *  This program is distributed in the hope that it will be useful,
  13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  *  GNU General Public License for more details.
  16  *  
  17  *  You should have received a copy of the GNU General Public License
  18  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  19  */
  20 
  21 #include "perf.h"
  22 
  23 sig_atomic_t keep_running = TRUE;
  24 
  25 /* allocates memory and gets numCPUs, total memory, and PerfFreq, number of disks... */
  26 void get_constants(PERF_DATA_BLOCK *data)
     /* [<][>][^][v][top][bottom][index][help] */
  27 {
  28     data->cpuInfo.numCPUs = sysconf(_SC_NPROCESSORS_ONLN) > 0 ? sysconf(_SC_NPROCESSORS_ONLN) : 1;
  29     data->PerfFreq = sysconf(_SC_CLK_TCK);
  30     init_mem_data(data);
  31     init_cpu_data(data);
  32     init_process_data(data);
  33     init_disk_data(data);
  34 
  35     return;
  36 }
  37 
  38 void output_num_instances(PerfCounter obj, int numInst, RuntimeSettings rt)
     /* [<][>][^][v][top][bottom][index][help] */
  39 {
  40     char key[NAME_LEN];
  41     char sdata[NAME_LEN];
  42 
  43     make_key(key, NAME_LEN, obj.index, "inst");
  44     memset(sdata, 0, NAME_LEN);
  45     sprintf(sdata, "%d", numInst);
  46     add_key(rt.cnames, key, sdata, TDB_INSERT);
  47 
  48     return;
  49 }
  50 
  51 void output_perf_desc(PerfCounter counter, RuntimeSettings rt)
     /* [<][>][^][v][top][bottom][index][help] */
  52 {
  53     char key[NAME_LEN];
  54     char sdata[NAME_LEN];
  55 
  56     /* First insert the counter name */
  57     make_key(key, NAME_LEN, counter.index, NULL);
  58     add_key(rt.cnames, key, counter.name, TDB_INSERT);
  59     /* Add the help string */
  60     make_key(key, NAME_LEN, counter.index + 1, NULL);
  61     add_key(rt.cnames, key, counter.help, TDB_INSERT);
  62     /* Add the relationships */
  63     make_key(key, NAME_LEN, counter.index, "rel");
  64     add_key(rt.cnames, key, counter.relationships, TDB_INSERT);
  65     /* Add type data if not PERF_OBJECT or PERF_INSTANCE */
  66     if(counter.record_type == PERF_COUNTER)
  67     {
  68         make_key(key, NAME_LEN, counter.index, "type");
  69         memset(sdata, 0, NAME_LEN);
  70         sprintf(sdata, "%d", counter.counter_type);
  71         add_key(rt.cnames, key, sdata, TDB_INSERT);
  72     }
  73 
  74     return;
  75 }
  76 
  77 void initialize(PERF_DATA_BLOCK *data, RuntimeSettings *rt, int argc, char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
  78 {
  79     memset(data, 0, sizeof(*data));
  80     memset(rt, 0, sizeof(*data));
  81 
  82     parse_flags(rt, argc, argv);
  83     setup_file_paths(rt);
  84 
  85     get_constants(data);
  86 
  87     if(rt->dflag == TRUE)
  88         daemonize(rt);
  89 
  90     output_mem_desc(data, *rt);
  91     output_cpu_desc(data, *rt);
  92     output_process_desc(data, *rt);
  93     output_disk_desc(data, *rt);
  94 
  95     return;
  96 }
  97 
  98 void refresh_perf_data_block(PERF_DATA_BLOCK *data, RuntimeSettings rt)
     /* [<][>][^][v][top][bottom][index][help] */
  99 {
 100     data->PerfTime100nSec = 0;
 101     get_meminfo(data);
 102     get_cpuinfo(data);
 103     get_processinfo(data);
 104     get_diskinfo(data);
 105     return;
 106 }
 107 
 108 void output_perf_counter(PerfCounter counter, unsigned long long data,
     /* [<][>][^][v][top][bottom][index][help] */
 109                          RuntimeSettings rt, int tdb_flags)
 110 {
 111     char key[NAME_LEN];
 112     char sdata[NAME_LEN];
 113     unsigned int size_mask;
 114 
 115     make_key(key, NAME_LEN, counter.index, NULL);
 116     memset(sdata, 0, NAME_LEN);
 117 
 118     size_mask = counter.counter_type & PERF_SIZE_VARIABLE_LEN;
 119 
 120     if(size_mask == PERF_SIZE_DWORD)
 121         sprintf(sdata, "%d", (unsigned int)data);
 122     else if(size_mask == PERF_SIZE_LARGE)
 123         sprintf(sdata, "%Lu", data);
 124 
 125     add_key(rt.cdata, key, sdata, tdb_flags);
 126 
 127     return;
 128 }
 129 
 130 void output_perf_instance(int parentObjInd,
     /* [<][>][^][v][top][bottom][index][help] */
 131                           int instanceInd,
 132                           void *instData,
 133                           size_t dsize,
 134                           char *name,
 135                           RuntimeSettings rt,
 136                           int tdb_flags)
 137 {
 138     char key[NAME_LEN];
 139     char sdata[NAME_LEN];
 140 
 141     memset(key, 0, NAME_LEN);
 142     sprintf(key, "%di%d", parentObjInd, instanceInd);
 143     add_key_raw(rt.cdata, key, instData, dsize, tdb_flags);
 144 
 145     /* encode name */
 146     memset(key, 0, NAME_LEN);
 147     sprintf(key, "%di%dname", parentObjInd, instanceInd);
 148     add_key(rt.cnames, key, name, tdb_flags);
 149 
 150     return;
 151 }
 152 
 153 void output_global_data(PERF_DATA_BLOCK *data, RuntimeSettings rt, int tdb_flags)
     /* [<][>][^][v][top][bottom][index][help] */
 154 {
 155     int i;
 156     char key[NAME_LEN];
 157     char sdata[NAME_LEN];
 158     
 159      /* Initialize BaseIndex */
 160     make_key(key, NAME_LEN, 1, NULL);
 161     memset(sdata, 0, NAME_LEN);
 162     sprintf(sdata, "%d", data->num_counters);
 163     add_key(rt.cnames, key, sdata, tdb_flags);
 164     /* Initialize PerfTime, PerfFreq and PerfTime100nSec */
 165     memset(sdata, 0, NAME_LEN);
 166     make_key(key, NAME_LEN, 0, "PerfTime");
 167     sprintf(sdata, "%Lu", data->PerfTime);
 168     add_key(rt.cdata, key, sdata, tdb_flags);
 169     make_key(key, NAME_LEN, 0, "PerfTime100nSec");
 170     memset(sdata, 0, NAME_LEN);
 171     sprintf(sdata, "%Lu", data->PerfTime100nSec);
 172     add_key(rt.cdata, key, sdata, tdb_flags);
 173     memset(sdata, 0, NAME_LEN);
 174     make_key(key, NAME_LEN, 0, "PerfFreq");
 175     sprintf(sdata, "%Lu", data->PerfFreq);
 176     add_key(rt.cnames, key, sdata, tdb_flags);
 177 
 178     return;
 179 }
 180 
 181 void output_perf_data_block(PERF_DATA_BLOCK *data, RuntimeSettings rt, int tdb_flags)
     /* [<][>][^][v][top][bottom][index][help] */
 182 {
 183     output_global_data(data, rt, tdb_flags);
 184     output_meminfo(data, rt, tdb_flags);
 185     output_cpuinfo(data, rt, tdb_flags);
 186     output_processinfo(data, rt, tdb_flags);
 187     output_diskinfo(data, rt, tdb_flags);
 188     return;
 189 }
 190 
 191 void update_counters(PERF_DATA_BLOCK *data, RuntimeSettings rt)
     /* [<][>][^][v][top][bottom][index][help] */
 192 {
 193     refresh_perf_data_block(data, rt);
 194     output_perf_data_block(data, rt, TDB_REPLACE);
 195 
 196     return;
 197 }
 198 
 199 int main(int argc, char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
 200 {
 201     PERF_DATA_BLOCK data;
 202     RuntimeSettings rt;
 203 
 204     initialize(&data, &rt, argc, argv);
 205 
 206     while(keep_running)
 207     {
 208         update_counters(&data, rt);
 209         sleep(1);
 210     }
 211     
 212     return 0;
 213 }

/* [<][>][^][v][top][bottom][index][help] */