WHATSNEW: Update changes since 3.3.0pre2.
[Samba.git] / examples / perfcounter / perf_writer.c
blob4260f9595d1295e35252497b3965781893b74bd7
1 /*
2 * Unix SMB/CIFS implementation.
3 * Performance Counter Daemon
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.
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.
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/>.
21 #include "perf.h"
23 sig_atomic_t keep_running = TRUE;
25 /* allocates memory and gets numCPUs, total memory, and PerfFreq, number of disks... */
26 void get_constants(PERF_DATA_BLOCK *data)
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);
35 return;
38 void output_num_instances(PerfCounter obj, int numInst, RuntimeSettings rt)
40 char key[NAME_LEN];
41 char sdata[NAME_LEN];
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);
48 return;
51 void output_perf_desc(PerfCounter counter, RuntimeSettings rt)
53 char key[NAME_LEN];
54 char sdata[NAME_LEN];
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)
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);
74 return;
77 void initialize(PERF_DATA_BLOCK *data, RuntimeSettings *rt, int argc, char **argv)
79 memset(data, 0, sizeof(*data));
80 memset(rt, 0, sizeof(*data));
82 parse_flags(rt, argc, argv);
83 setup_file_paths(rt);
85 get_constants(data);
87 if(rt->dflag == TRUE)
88 daemonize(rt);
90 output_mem_desc(data, *rt);
91 output_cpu_desc(data, *rt);
92 output_process_desc(data, *rt);
93 output_disk_desc(data, *rt);
95 return;
98 void refresh_perf_data_block(PERF_DATA_BLOCK *data, RuntimeSettings rt)
100 data->PerfTime100nSec = 0;
101 get_meminfo(data);
102 get_cpuinfo(data);
103 get_processinfo(data);
104 get_diskinfo(data);
105 return;
108 void output_perf_counter(PerfCounter counter, unsigned long long data,
109 RuntimeSettings rt, int tdb_flags)
111 char key[NAME_LEN];
112 char sdata[NAME_LEN];
113 unsigned int size_mask;
115 make_key(key, NAME_LEN, counter.index, NULL);
116 memset(sdata, 0, NAME_LEN);
118 size_mask = counter.counter_type & PERF_SIZE_VARIABLE_LEN;
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);
125 add_key(rt.cdata, key, sdata, tdb_flags);
127 return;
130 void output_perf_instance(int parentObjInd,
131 int instanceInd,
132 void *instData,
133 size_t dsize,
134 char *name,
135 RuntimeSettings rt,
136 int tdb_flags)
138 char key[NAME_LEN];
139 char sdata[NAME_LEN];
141 memset(key, 0, NAME_LEN);
142 sprintf(key, "%di%d", parentObjInd, instanceInd);
143 add_key_raw(rt.cdata, key, instData, dsize, tdb_flags);
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);
150 return;
153 void output_global_data(PERF_DATA_BLOCK *data, RuntimeSettings rt, int tdb_flags)
155 int i;
156 char key[NAME_LEN];
157 char sdata[NAME_LEN];
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);
178 return;
181 void output_perf_data_block(PERF_DATA_BLOCK *data, RuntimeSettings rt, int tdb_flags)
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;
191 void update_counters(PERF_DATA_BLOCK *data, RuntimeSettings rt)
193 refresh_perf_data_block(data, rt);
194 output_perf_data_block(data, rt, TDB_REPLACE);
196 return;
199 int main(int argc, char **argv)
201 PERF_DATA_BLOCK data;
202 RuntimeSettings rt;
204 initialize(&data, &rt, argc, argv);
206 while(keep_running)
208 update_counters(&data, rt);
209 sleep(1);
212 return 0;