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 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/>.
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
);
32 init_process_data(data
);
38 void output_num_instances(PerfCounter obj
, int numInst
, RuntimeSettings rt
)
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
);
51 void output_perf_desc(PerfCounter counter
, RuntimeSettings rt
)
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
);
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
);
90 output_mem_desc(data
, *rt
);
91 output_cpu_desc(data
, *rt
);
92 output_process_desc(data
, *rt
);
93 output_disk_desc(data
, *rt
);
98 void refresh_perf_data_block(PERF_DATA_BLOCK
*data
, RuntimeSettings rt
)
100 data
->PerfTime100nSec
= 0;
103 get_processinfo(data
);
108 void output_perf_counter(PerfCounter counter
, unsigned long long data
,
109 RuntimeSettings rt
, int tdb_flags
)
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
);
130 void output_perf_instance(int parentObjInd
,
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
);
146 memset(key
, 0, NAME_LEN
);
147 sprintf(key
, "%di%dname", parentObjInd
, instanceInd
);
148 add_key(rt
.cnames
, key
, name
, tdb_flags
);
153 void output_global_data(PERF_DATA_BLOCK
*data
, RuntimeSettings rt
, int tdb_flags
)
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
);
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
);
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
);
199 int main(int argc
, char **argv
)
201 PERF_DATA_BLOCK data
;
204 initialize(&data
, &rt
, argc
, argv
);
208 update_counters(&data
, rt
);