2 * Unix SMB/CIFS implementation.
3 * Performance Counter Daemon
5 * Copyright (C) Marcin Krzysztof Porwit 2005
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.
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
);
33 init_process_data(data
);
39 void output_num_instances(PerfCounter obj
, int numInst
, RuntimeSettings rt
)
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
);
52 void output_perf_desc(PerfCounter counter
, RuntimeSettings rt
)
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
);
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
);
91 output_mem_desc(data
, *rt
);
92 output_cpu_desc(data
, *rt
);
93 output_process_desc(data
, *rt
);
94 output_disk_desc(data
, *rt
);
99 void refresh_perf_data_block(PERF_DATA_BLOCK
*data
, RuntimeSettings rt
)
101 data
->PerfTime100nSec
= 0;
104 get_processinfo(data
);
109 void output_perf_counter(PerfCounter counter
, unsigned long long data
,
110 RuntimeSettings rt
, int tdb_flags
)
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
);
131 void output_perf_instance(int parentObjInd
,
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
);
147 memset(key
, 0, NAME_LEN
);
148 sprintf(key
, "%di%dname", parentObjInd
, instanceInd
);
149 add_key(rt
.cnames
, key
, name
, tdb_flags
);
154 void output_global_data(PERF_DATA_BLOCK
*data
, RuntimeSettings rt
, int tdb_flags
)
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
);
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
);
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
);
200 int main(int argc
, char **argv
)
202 PERF_DATA_BLOCK data
;
205 initialize(&data
, &rt
, argc
, argv
);
209 update_counters(&data
, rt
);