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 extern sig_atomic_t keep_running;
32 void add_key_raw(TDB_CONTEXT *db, char *keystring, void *databuf, size_t datasize, int flags)
37 key.dsize = strlen(keystring);
39 data.dsize = datasize;
40 fprintf(stderr, "doing insert of [%x] with key [%s] into [%s]\n",
45 tdb_store(db, key, data, flags);
48 void add_key(TDB_CONTEXT *db, char *keystring, char *datastring, int flags)
53 key.dsize = strlen(keystring);
54 data.dptr = datastring;
55 data.dsize = strlen(datastring);
56 /* fprintf(stderr, "doing insert of [%s] with key [%s] into [%s]\n",
61 tdb_store(db, key, data, flags);
64 void make_key(char *buf, int buflen, int key_part1, char *key_part2)
66 memset(buf, 0, buflen);
68 sprintf(buf, "%d%s", key_part1, key_part2);
70 sprintf(buf, "%d", key_part1);
75 void usage(char *progname)
77 fprintf(stderr, "Usage: %s [-d] [-f <file_path>].\n", progname);
78 fprintf(stderr, "\t-d: run as a daemon.\n");
79 fprintf(stderr, "\t-f <file_path>: path where the TDB files reside.\n");
80 fprintf(stderr, "\t\tDEFAULT is /tmp/counters\n");
84 void parse_flags(RuntimeSettings *rt, int argc, char **argv)
88 while((flag = getopt(argc, argv, "df:")) != -1)
99 memcpy(rt->dbDir, optarg, strlen(optarg));
112 void setup_file_paths(RuntimeSettings *rt)
116 if(strlen(rt->dbDir) == 0)
118 /* No file path was passed in, use default */
119 sprintf(rt->dbDir, "/tmp/counters");
122 sprintf(rt->nameFile, "%s/names.tdb", rt->dbDir);
123 sprintf(rt->counterFile, "%s/data.tdb", rt->dbDir);
125 mkdir(rt->dbDir, O_RDWR);
126 rt->cnames = tdb_open(rt->nameFile, 0, TDB_CLEAR_IF_FIRST, O_RDWR | O_CREAT, 0644);
127 rt->cdata = tdb_open(rt->counterFile, 0, TDB_CLEAR_IF_FIRST, O_RDWR | O_CREAT, 0644);
129 if(rt->cnames == NULL || rt->cdata == NULL)
131 perror("setup_file_paths");
138 void sigterm_handler()
140 keep_running = FALSE;
144 void daemonize(RuntimeSettings *rt)
150 /* Check if we're already a daemon */
159 /* we're the parent */
160 tdb_close(rt->cnames);
161 tdb_close(rt->cdata);
165 /* get a new session */
172 /* close file descriptors */
174 close(STDOUT_FILENO);
175 close(STDERR_FILENO);
177 /* And reopen them as safe defaults */
178 fd = open("/dev/null", O_RDONLY);
184 fd = open("/dev/null", O_WRONLY);
190 fd = open("/dev/null", O_WRONLY);
198 signal(SIGINT, SIG_IGN);
199 signal(SIGHUP, SIG_IGN);
200 signal(SIGTERM, sigterm_handler);
205 int get_counter_id(PERF_DATA_BLOCK *data)
207 data->counter_id += 2;
208 data->num_counters++;
210 return data->counter_id;
213 void init_perf_counter(PerfCounter *counter,
221 counter->index = index;
222 memcpy(counter->name, name, strlen(name));
223 memcpy(counter->help, help, strlen(help));
224 counter->counter_type = counter_type;
225 counter->record_type = record_type;
230 sprintf(counter->relationships, "p");
233 sprintf(counter->relationships, "c[%d]", parent->index);
236 sprintf(counter->relationships, "i[%d]", parent->index);
239 perror("init_perf_counter: unknown record type");