summaryrefslogtreecommitdiffstats
path: root/source3/modules/perfcount_test.c
blob: b72ac9f7b5c238a09d518822f5ee9f8bd991b376 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * Unix SMB/CIFS implementation.
 * Test module for perfcounters
 *
 * Copyright (C) Todd Stecher 2008
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"

#define PARM_PC_TEST_TYPE		"pc_test"
#define PARM_DUMPON_COUNT		"count"
#define PARM_DUMPON_COUNT_DEFAULT	50

struct perfcount_test_identity {
	uid_t uid;
	char *user;
	char *domain;
};

struct perfcount_test_counter {
	int op;
	int sub_op;
	int ioctl;
	uint64_t bytes_in;
	uint64_t bytes_out;
	int count;

	struct perfcount_test_counter *next;
	struct perfcount_test_counter *prev;
};

struct perfcount_test_context {

	/* wip:  identity */
	struct perfcount_test_identity *id;
	struct perfcount_test_counter *ops;
};

#define MAX_OP 256
struct perfcount_test_counter *g_list[MAX_OP];

int count;

/* determine frequency of dumping results */
int count_mod = 1;

static void perfcount_test_add_counters(struct perfcount_test_context *ctxt)
{
	struct perfcount_test_counter *head;
	struct perfcount_test_counter *ptc;
	struct perfcount_test_counter *tmp;
	bool found;

	for (ptc = ctxt->ops; ptc != NULL; ) {

		found = false;

		if (ptc->op > MAX_OP)
			continue;

		for (head = g_list[ptc->op]; head != NULL; head = head->next) {
			if ((ptc->sub_op == head->sub_op) &&
			    (ptc->ioctl == head->ioctl)) {
				head->bytes_in += ptc->bytes_in;
				head->bytes_out += ptc->bytes_out;
				head->count++;
				tmp = ptc->next;
				DLIST_REMOVE(ctxt->ops, ptc);
				SAFE_FREE(ptc);
				ptc = tmp;
				found = true;
				break;
			}
		}

		/* not in global tracking list - add it */
		if (!found) {
			tmp = ptc->next;
			DLIST_REMOVE(ctxt->ops, ptc);
			ptc->count = 1;
			DLIST_ADD(g_list[ptc->op], ptc);
			ptc = tmp;
		}
	}

}

#if 0

static void perfcount_test_dump_id(struct perfcount_test_identity *id, int lvl)
{
	if (!id)
		return;

	DEBUG(lvl,("uid - %d\n", id->uid));
	DEBUG(lvl,("user - %s\n", id->user));
	DEBUG(lvl,("domain - %s\n", id->domain));
}

#endif

static const char *trans_subop_table[] = {
	"unknown", "trans:create", "trans:ioctl", "trans:set sd",
	"trans:change notify", "trans: rename", "trans:get sd",
	"trans:get quota", "trans:set quota"
};

static const char *trans2_subop_table[] = {
	"trans2:open", "trans2:find first", "trans2:find next",
	"trans2:q fsinfo", "trans2:set fsinfo", "trans2:q path info",
	"trans2:set pathinfo", "trans2:fs ctl", "trans2: io ctl",
	"trans2:find notify first", "trans2:find notify next",
	"trans2:mkdir", "trans2:sess setup", "trans2:get dfs referral",
	"trans2:report dfs inconsistent"
};

static const char *smb_subop_name(int op, int subop)
{
	/* trans */
	if (op == 0x25) {
		if (subop > sizeof(trans_subop_table) /
		    sizeof(trans_subop_table[0])) {
			return "unknown";
		}
		return trans_subop_table[subop];
	} else if (op == 0x32) {
		if (subop > sizeof(trans2_subop_table) /
		    sizeof(trans2_subop_table[0])) {
			return "unknown";
		}
		return trans2_subop_table[subop];
	}

	return "unknown";
}

static void perfcount_test_dump_counter(struct perfcount_test_counter *ptc,
					int lvl)
{
	DEBUG(lvl, ("OP: %s\n", smb_fn_name(ptc->op)));
	if (ptc->sub_op > 0) {
		DEBUG(lvl, ("SUBOP: %s\n",
			smb_subop_name(ptc->op, ptc->sub_op)));
	}

	if (ptc->ioctl > 0) {
		DEBUG(lvl, ("IOCTL: %d\n", ptc->ioctl));
	}

	DEBUG(lvl, ("Count: %d\n\n", ptc->count));
}

static void perfcount_test_dump_counters(void)
{
	int i;
	struct perfcount_test_counter *head;

	count_mod = lp_parm_int(0, PARM_PC_TEST_TYPE, PARM_DUMPON_COUNT,
	    PARM_DUMPON_COUNT_DEFAULT);

	if ((count++ % count_mod) != 0)
		return;

	DEBUG(0,("#####  Dumping Performance Counters #####\n"));

	for (i=0; i < 256; i++) {
	       for (head = g_list[i]; head != NULL; head = head->next) {
		       perfcount_test_dump_counter(head, 0);
		       head->prev = NULL;
		       SAFE_FREE(head->prev);
	       }
	       SAFE_FREE(head);
	}
}

/*  operations */
static void perfcount_test_start(struct smb_perfcount_data *pcd)
{
	struct perfcount_test_context *ctxt;
	struct perfcount_test_counter *ctr;
	/*
	 * there shouldn't already be a context here - if so,
	 * there's an unbalanced call to start / end.
	 */
	if (pcd->context) {
		DEBUG(0,("perfcount_test_start - starting "
			 "initialized context - %p\n", pcd));
		return;
	}

	ctxt = SMB_MALLOC_P(struct perfcount_test_context);
	if (!ctxt)
		return;

	ZERO_STRUCTP(ctxt);

	/* create 'default' context */
	ctr = SMB_MALLOC_P(struct perfcount_test_counter);
	if (!ctr) {
		SAFE_FREE(ctxt);
		return;
	}

	ZERO_STRUCTP(ctr);
	ctr->op = ctr->sub_op = ctr->ioctl = -1;
	DLIST_ADD(ctxt->ops, ctr);

	pcd->context = (void*)ctxt;
}

static void perfcount_test_add(struct smb_perfcount_data *pcd)
{
	struct perfcount_test_context *ctxt =
		(struct perfcount_test_context *)pcd->context;
	struct perfcount_test_counter *ctr;

        if (pcd->context == NULL)
                return;

	ctr = SMB_MALLOC_P(struct perfcount_test_counter);
	if (!ctr) {
		return;
	}

	DLIST_ADD(ctxt->ops, ctr);

}

static void perfcount_test_set_op(struct smb_perfcount_data *pcd, int op)
{
	struct perfcount_test_context *ctxt =
		(struct perfcount_test_context *)pcd->context;

        if (pcd->context == NULL)
                return;

	ctxt->ops->op = op;
}

static void perfcount_test_set_subop(struct smb_perfcount_data *pcd, int sub_op)
{
	struct perfcount_test_context *ctxt =
		(struct perfcount_test_context *)pcd->context;

        if (pcd->context == NULL)
                return;

	ctxt->ops->sub_op = sub_op;
}

static void perfcount_test_set_ioctl(struct smb_perfcount_data *pcd, int io_ctl)
{
	struct perfcount_test_context *ctxt =
		(struct perfcount_test_context *)pcd->context;
        if (pcd->context == NULL)
                return;

	ctxt->ops->ioctl = io_ctl;
}

static void perfcount_test_set_msglen_in(struct smb_perfcount_data *pcd,
					 uint64_t bytes_in)
{
	struct perfcount_test_context *ctxt =
		(struct perfcount_test_context *)pcd->context;
        if (pcd->context == NULL)
                return;

	ctxt->ops->bytes_in = bytes_in;
}

static void perfcount_test_set_msglen_out(struct smb_perfcount_data *pcd,
					  uint64_t bytes_out)
{
	struct perfcount_test_context *ctxt =
		(struct perfcount_test_context *)pcd->context;

        if (pcd->context == NULL)
                return;

	ctxt->ops->bytes_out = bytes_out;
}

static void perfcount_test_copy_context(struct smb_perfcount_data *pcd,
				        struct smb_perfcount_data *new_pcd)
{
	struct perfcount_test_context *ctxt =
		(struct perfcount_test_context *)pcd->context;
	struct perfcount_test_context *new_ctxt;

	struct perfcount_test_counter *ctr;
	struct perfcount_test_counter *new_ctr;

        if (pcd->context == NULL)
                return;

	new_ctxt = SMB_MALLOC_P(struct perfcount_test_context);
	if (!new_ctxt) {
		return;
	}

	memcpy(new_ctxt, ctxt, sizeof(struct perfcount_test_context));

	for (ctr = ctxt->ops; ctr != NULL; ctr = ctr->next) {
		new_ctr = SMB_MALLOC_P(struct perfcount_test_counter);
		if (!new_ctr) {
			goto error;
		}

		memcpy(new_ctr, ctr, sizeof(struct perfcount_test_counter));
		new_ctr->next = NULL;
		new_ctr->prev = NULL;
		DLIST_ADD(new_ctxt->ops, new_ctr);
	}

	new_pcd->context = new_ctxt;
	return;

error:

	for (ctr = new_ctxt->ops; ctr != NULL; ) {
		new_ctr = ctr->next;
		SAFE_FREE(ctr);
		ctr = new_ctr;
	}

	SAFE_FREE(new_ctxt);
}

/*
 * For perf reasons, its best to use some global state
 * when an operation is deferred, we need to alloc a copy.
 */
static void perfcount_test_defer_op(struct smb_perfcount_data *pcd,
				    struct smb_perfcount_data *def_pcd)
{
	/* we don't do anything special to deferred ops */
	return;
}

static void perfcount_test_end(struct smb_perfcount_data *pcd)
{
	struct perfcount_test_context *ctxt =
		(struct perfcount_test_context *)pcd->context;
        if (pcd->context == NULL)
                return;

	/* @bug - we don't store outbytes right for chained cmds */
	perfcount_test_add_counters(ctxt);
	perfcount_test_dump_counters();
	pcd->context = NULL;
	SAFE_FREE(ctxt);
}


static struct smb_perfcount_handlers perfcount_test_handlers = {
	perfcount_test_start,
	perfcount_test_add,
	perfcount_test_set_op,
	perfcount_test_set_subop,
	perfcount_test_set_ioctl,
	perfcount_test_set_msglen_in,
	perfcount_test_set_msglen_out,
	perfcount_test_copy_context,
	perfcount_test_defer_op,
	perfcount_test_end
};

NTSTATUS perfcount_test_init(void)
{
	return smb_register_perfcounter(SMB_PERFCOUNTER_INTERFACE_VERSION,
					"pc_test", &perfcount_test_handlers);
}