Remove port 445 so NBT works correctly.
[Samba.git] / examples / perfcounter / perf_writer.c
blob00e47bdabafabb9876a5738bcd316e6782973a9d
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 2 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, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "perf.h"
24 sig_atomic_t keep_running = TRUE;
26 /* allocates memory and gets numCPUs, total memory, and PerfFreq, number of disks... */
27 void get_constants(PERF_DATA_BLOCK *data)
29 data->cpuInfo.numCPUs = sysconf(_SC_NPROCESSORS_ONLN) > 0 ? sysconf(_SC_NPROCESSORS_ONLN) : 1;
30 data->PerfFreq = sysconf(_SC_CLK_TCK);
31 init_mem_data(data);
32 init_cpu_data(data);
33 init_process_data(data);
34 init_disk_data(data);
36 return;
39 void output_num_instances(PerfCounter obj, int numInst, RuntimeSettings rt)
41 char key[NAME_LEN];
42 char sdata[NAME_LEN];
44 make_key(key, NAME_LEN, obj.index, "inst");
45 memset(sdata, 0, NAME_LEN);
46 sprintf(sdata, "%d", numInst);
47 add_key(rt.cnames, key, sdata, TDB_INSERT);
49 return;
52 void output_perf_desc(PerfCounter counter, RuntimeSettings rt)
54 char key[NAME_LEN];
55 char sdata[NAME_LEN];
57 /* First insert the counter name */
58 make_key(key, NAME_LEN, counter.index, NULL);
59 add_key(rt.cnames, key, counter.name, TDB_INSERT);
60 /* Add the help string */
61 make_key(key, NAME_LEN, counter.index + 1, NULL);
62 add_key(rt.cnames, key, counter.help, TDB_INSERT);
63 /* Add the relationships */
64 make_key(key, NAME_LEN, counter.index, "rel");
65 add_key(rt.cnames, key, counter.relationships, TDB_INSERT);
66 /* Add type data if not PERF_OBJECT or PERF_INSTANCE */
67 if(counter.record_type == PERF_COUNTER)
69 make_key(key, NAME_LEN, counter.index, "type");
70 memset(sdata, 0, NAME_LEN);
71 sprintf(sdata, "%d", counter.counter_type);
72 add_key(rt.cnames, key, sdata, TDB_INSERT);
75 return;
78 void initialize(PERF_DATA_BLOCK *data, RuntimeSettings *rt, int argc, char **argv)
80 memset(data, 0, sizeof(*data));
81 memset(rt, 0, sizeof(*data));
83 parse_flags(rt, argc, argv);
84 setup_file_paths(rt);
86 get_constants(data);
88 if(rt->dflag == TRUE)
89 daemonize(rt);
91 output_mem_desc(data, *rt);
92 output_cpu_desc(data, *rt);
93 output_process_desc(data, *rt);
94 output_disk_desc(data, *rt);
96 return;
99 void refresh_perf_data_block(PERF_DATA_BLOCK *data, RuntimeSettings rt)
101 data->PerfTime100nSec = 0;
102 get_meminfo(data);
103 get_cpuinfo(data);
104 get_processinfo(data);
105 get_diskinfo(data);
106 return;
109 void output_perf_counter(PerfCounter counter, unsigned long long data,
110 RuntimeSettings rt, int tdb_flags)
112 char key[NAME_LEN];
113 char sdata[NAME_LEN];
114 unsigned int size_mask;
116 make_key(key, NAME_LEN, counter.index, NULL);
117 memset(sdata, 0, NAME_LEN);
119 size_mask = counter.counter_type & PERF_SIZE_VARIABLE_LEN;
121 if(size_mask == PERF_SIZE_DWORD)
122 sprintf(sdata, "%d", (unsigned int)data);
123 else if(size_mask == PERF_SIZE_LARGE)
124 sprintf(sdata, "%Lu", data);
126 add_key(rt.cdata, key, sdata, tdb_flags);
128 return;
131 void output_perf_instance(int parentObjInd,
132 int instanceInd,
133 void *instData,
134 size_t dsize,
135 char *name,
136 RuntimeSettings rt,
137 int tdb_flags)
139 char key[NAME_LEN];
140 char sdata[NAME_LEN];
142 memset(key, 0, NAME_LEN);
143 sprintf(key, "%di%d", parentObjInd, instanceInd);
144 add_key_raw(rt.cdata, key, instData, dsize, tdb_flags);
146 /* encode name */
147 memset(key, 0, NAME_LEN);
148 sprintf(key, "%di%dname", parentObjInd, instanceInd);
149 add_key(rt.cnames, key, name, tdb_flags);
151 return;
154 void output_global_data(PERF_DATA_BLOCK *data, RuntimeSettings rt, int tdb_flags)
156 int i;
157 char key[NAME_LEN];
158 char sdata[NAME_LEN];
160 /* Initialize BaseIndex */
161 make_key(key, NAME_LEN, 1, NULL);
162 memset(sdata, 0, NAME_LEN);
163 sprintf(sdata, "%d", data->num_counters);
164 add_key(rt.cnames, key, sdata, tdb_flags);
165 /* Initialize PerfTime, PerfFreq and PerfTime100nSec */
166 memset(sdata, 0, NAME_LEN);
167 make_key(key, NAME_LEN, 0, "PerfTime");
168 sprintf(sdata, "%Lu", data->PerfTime);
169 add_key(rt.cdata, key, sdata, tdb_flags);
170 make_key(key, NAME_LEN, 0, "PerfTime100nSec");
171 memset(sdata, 0, NAME_LEN);
172 sprintf(sdata, "%Lu", data->PerfTime100nSec);
173 add_key(rt.cdata, key, sdata, tdb_flags);
174 memset(sdata, 0, NAME_LEN);
175 make_key(key, NAME_LEN, 0, "PerfFreq");
176 sprintf(sdata, "%Lu", data->PerfFreq);
177 add_key(rt.cnames, key, sdata, tdb_flags);
179 return;
182 void output_perf_data_block(PERF_DATA_BLOCK *data, RuntimeSettings rt, int tdb_flags)
184 output_global_data(data, rt, tdb_flags);
185 output_meminfo(data, rt, tdb_flags);
186 output_cpuinfo(data, rt, tdb_flags);
187 output_processinfo(data, rt, tdb_flags);
188 output_diskinfo(data, rt, tdb_flags);
189 return;
192 void update_counters(PERF_DATA_BLOCK *data, RuntimeSettings rt)
194 refresh_perf_data_block(data, rt);
195 output_perf_data_block(data, rt, TDB_REPLACE);
197 return;
200 int main(int argc, char **argv)
202 PERF_DATA_BLOCK data;
203 RuntimeSettings rt;
205 initialize(&data, &rt, argc, argv);
207 while(keep_running)
209 update_counters(&data, rt);
210 sleep(1);
213 return 0;