summaryrefslogtreecommitdiffstats
path: root/examples/perfcounter/perf_writer_util.c
blob: 9cf12ab79207203f878ffb1cea43dbf61e36b68c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* 
 *  Unix SMB/CIFS implementation.
 *  Performance Counter Daemon
 *
 *  Copyright (C) Marcin Krzysztof Porwit    2005
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "perf.h"

extern sig_atomic_t keep_running;

void fatal(char *msg)
{
  perror(msg);
  exit(1);
}

void add_key_raw(TDB_CONTEXT *db, char *keystring, void *databuf, size_t datasize, int flags)
{
  TDB_DATA key, data;

  key.dptr = keystring;
  key.dsize = strlen(keystring);
  data.dptr = databuf;
  data.dsize = datasize;

  tdb_store(db, key, data, flags);
}

void add_key(TDB_CONTEXT *db, char *keystring, char *datastring, int flags)
{
  TDB_DATA key, data;

  key.dptr = keystring;
  key.dsize = strlen(keystring);
  data.dptr = datastring;
  data.dsize = strlen(datastring);

  tdb_store(db, key, data, flags);
}

void make_key(char *buf, int buflen, int key_part1, char *key_part2)
{
    memset(buf, 0, buflen);
    if(key_part2 != NULL)
	sprintf(buf, "%d%s", key_part1, key_part2);
    else
	sprintf(buf, "%d", key_part1);

    return;
}

void usage(char *progname)
{
    fprintf(stderr, "Usage: %s [-d] [-f <file_path>].\n", progname);
    fprintf(stderr, "\t-d: run as a daemon.\n");
    fprintf(stderr, "\t-f <file_path>: path where the TDB files reside.\n");
    fprintf(stderr, "\t\tDEFAULT is /var/lib/samba/perfmon\n");
    exit(1);
}

void parse_flags(RuntimeSettings *rt, int argc, char **argv)
{
    int flag;
    
    while((flag = getopt(argc, argv, "df:")) != -1)
    {
	switch(flag)
	{
	    case 'd':
	    {
		rt->dflag = TRUE;
		break;
	    }
	    case 'f':
	    {
		memcpy(rt->dbDir, optarg, strlen(optarg));
		break;
	    }
	    default:
	    {
		usage(argv[0]);
	    }
	}
    }

    return;
}

void setup_file_paths(RuntimeSettings *rt)
{
    int status;

    if(strlen(rt->dbDir) == 0)
    {
	/* No file path was passed in, use default */
	sprintf(rt->dbDir, "/var/lib/samba/perfmon");
    }

    sprintf(rt->nameFile, "%s/names.tdb", rt->dbDir);
    sprintf(rt->counterFile, "%s/data.tdb", rt->dbDir);

    mkdir(rt->dbDir, 0755);
    rt->cnames = tdb_open(rt->nameFile, 0, TDB_CLEAR_IF_FIRST, O_RDWR | O_CREAT, 0644);
    rt->cdata = tdb_open(rt->counterFile, 0, TDB_CLEAR_IF_FIRST, O_RDWR | O_CREAT, 0644);

    if(rt->cnames == NULL || rt->cdata == NULL)
    {
	perror("setup_file_paths");
	exit(1);
    }

    return;
}

void sigterm_handler()
{
    keep_running = FALSE;
    return;
}

void daemonize(RuntimeSettings *rt)
{
    pid_t pid;
    int i;
    int fd;

    /* Check if we're already a daemon */
    if(getppid() == 1)
	return;
    pid = fork();
    if(pid < 0)
	/* can't fork */
	exit(1);
    else if(pid > 0)
    {
	/* we're the parent */
	tdb_close(rt->cnames);
	tdb_close(rt->cdata);
	exit(0);
    }

    /* get a new session */
    if(setsid() == -1)
	exit(2);

    /* Change CWD */
    chdir("/");

    /* close file descriptors */
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    /* And reopen them as safe defaults */
    fd = open("/dev/null", O_RDONLY);
    if(fd != 0)
    {
	dup2(fd, 0);
	close(fd);
    }
    fd = open("/dev/null", O_WRONLY);
    if(fd != 1)
    {
	dup2(fd, 1);
	close(fd);
    }
    fd = open("/dev/null", O_WRONLY);
    if(fd != 2)
    {
	dup2(fd, 2);
	close(fd);
    }

    /* handle signals */
    signal(SIGINT, SIG_IGN);
    signal(SIGHUP, SIG_IGN);
    signal(SIGTERM, sigterm_handler);

    return;
}

int get_counter_id(PERF_DATA_BLOCK *data)
{
    data->counter_id += 2;
    data->num_counters++;

    return data->counter_id;
}

void init_perf_counter(PerfCounter *counter,
		       PerfCounter *parent,
		       unsigned int index,
		       char *name,
		       char *help,
		       int counter_type,
		       int record_type)
{
    counter->index = index;
    memcpy(counter->name, name, strlen(name));
    memcpy(counter->help, help, strlen(help));
    counter->counter_type = counter_type;
    counter->record_type = record_type;

    switch(record_type)
    {
	case PERF_OBJECT:
	    sprintf(counter->relationships, "p");
	    break;
	case PERF_COUNTER:
	    sprintf(counter->relationships, "c[%d]", parent->index);
	    break;
	case PERF_INSTANCE:
	    sprintf(counter->relationships, "i[%d]", parent->index);
	    break;
	default:
	    perror("init_perf_counter: unknown record type");
	    exit(1);
    }

    return;
}