WHATSNEW: Update changes.
[Samba.git] / source3 / modules / perfcount_test.c
blob67be4aa9c610b599bb6e65bbf890a9b0b72effc8
1 /*
2 * Unix SMB/CIFS implementation.
3 * Test module for perfcounters
5 * Copyright (C) Todd Stecher 2008
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/>.
21 #include "includes.h"
23 #define PARM_PC_TEST_TYPE "pc_test"
24 #define PARM_DUMPON_COUNT "count"
25 #define PARM_DUMPON_COUNT_DEFAULT 50
27 struct perfcount_test_identity {
28 uid_t uid;
29 char *user;
30 char *domain;
33 struct perfcount_test_counter {
34 int op;
35 int sub_op;
36 int ioctl;
37 uint64_t bytes_in;
38 uint64_t bytes_out;
39 int count;
41 struct perfcount_test_counter *next;
42 struct perfcount_test_counter *prev;
45 struct perfcount_test_context {
47 /* wip: identity */
48 struct perfcount_test_identity *id;
49 struct perfcount_test_counter *ops;
52 #define MAX_OP 256
53 struct perfcount_test_counter *g_list[MAX_OP];
55 int count;
57 /* determine frequency of dumping results */
58 int count_mod = 1;
60 static void perfcount_test_add_counters(struct perfcount_test_context *ctxt)
62 struct perfcount_test_counter *head;
63 struct perfcount_test_counter *ptc;
64 struct perfcount_test_counter *tmp;
65 bool found;
67 for (ptc = ctxt->ops; ptc != NULL; ) {
69 found = false;
71 if (ptc->op > MAX_OP)
72 continue;
74 for (head = g_list[ptc->op]; head != NULL; head = head->next) {
75 if ((ptc->sub_op == head->sub_op) &&
76 (ptc->ioctl == head->ioctl)) {
77 head->bytes_in += ptc->bytes_in;
78 head->bytes_out += ptc->bytes_out;
79 head->count++;
80 tmp = ptc->next;
81 DLIST_REMOVE(ctxt->ops, ptc);
82 SAFE_FREE(ptc);
83 ptc = tmp;
84 found = true;
85 break;
89 /* not in global tracking list - add it */
90 if (!found) {
91 tmp = ptc->next;
92 DLIST_REMOVE(ctxt->ops, ptc);
93 ptc->count = 1;
94 DLIST_ADD(g_list[ptc->op], ptc);
95 ptc = tmp;
101 #if 0
103 static void perfcount_test_dump_id(struct perfcount_test_identity *id, int lvl)
105 if (!id)
106 return;
108 DEBUG(lvl,("uid - %d\n", id->uid));
109 DEBUG(lvl,("user - %s\n", id->user));
110 DEBUG(lvl,("domain - %s\n", id->domain));
113 #endif
115 static const char *trans_subop_table[] = {
116 "unknown", "trans:create", "trans:ioctl", "trans:set sd",
117 "trans:change notify", "trans: rename", "trans:get sd",
118 "trans:get quota", "trans:set quota"
121 static const char *trans2_subop_table[] = {
122 "trans2:open", "trans2:find first", "trans2:find next",
123 "trans2:q fsinfo", "trans2:set fsinfo", "trans2:q path info",
124 "trans2:set pathinfo", "trans2:fs ctl", "trans2: io ctl",
125 "trans2:find notify first", "trans2:find notify next",
126 "trans2:mkdir", "trans2:sess setup", "trans2:get dfs referral",
127 "trans2:report dfs inconsistent"
130 static const char *smb_subop_name(int op, int subop)
132 /* trans */
133 if (op == 0x25) {
134 if (subop > sizeof(trans_subop_table) /
135 sizeof(trans_subop_table[0])) {
136 return "unknown";
138 return trans_subop_table[subop];
139 } else if (op == 0x32) {
140 if (subop > sizeof(trans2_subop_table) /
141 sizeof(trans2_subop_table[0])) {
142 return "unknown";
144 return trans2_subop_table[subop];
147 return "unknown";
150 static void perfcount_test_dump_counter(struct perfcount_test_counter *ptc,
151 int lvl)
153 DEBUG(lvl, ("OP: %s\n", smb_fn_name(ptc->op)));
154 if (ptc->sub_op > 0) {
155 DEBUG(lvl, ("SUBOP: %s\n",
156 smb_subop_name(ptc->op, ptc->sub_op)));
159 if (ptc->ioctl > 0) {
160 DEBUG(lvl, ("IOCTL: %d\n", ptc->ioctl));
163 DEBUG(lvl, ("Count: %d\n\n", ptc->count));
166 static void perfcount_test_dump_counters(void)
168 int i;
169 struct perfcount_test_counter *head;
171 count_mod = lp_parm_int(0, PARM_PC_TEST_TYPE, PARM_DUMPON_COUNT,
172 PARM_DUMPON_COUNT_DEFAULT);
174 if ((count++ % count_mod) != 0)
175 return;
177 DEBUG(0,("##### Dumping Performance Counters #####\n"));
179 for (i=0; i < MAX_OP; i++) {
180 struct perfcount_test_counter *next;
181 for (head = g_list[i]; head != NULL; head = next) {
182 next = head->next;
183 perfcount_test_dump_counter(head, 0);
184 SAFE_FREE(head);
186 g_list[i] = NULL;
190 /* operations */
191 static void perfcount_test_start(struct smb_perfcount_data *pcd)
193 struct perfcount_test_context *ctxt;
194 struct perfcount_test_counter *ctr;
196 * there shouldn't already be a context here - if so,
197 * there's an unbalanced call to start / end.
199 if (pcd->context) {
200 DEBUG(0,("perfcount_test_start - starting "
201 "initialized context - %p\n", pcd));
202 return;
205 ctxt = SMB_MALLOC_P(struct perfcount_test_context);
206 if (!ctxt)
207 return;
209 ZERO_STRUCTP(ctxt);
211 /* create 'default' context */
212 ctr = SMB_MALLOC_P(struct perfcount_test_counter);
213 if (!ctr) {
214 SAFE_FREE(ctxt);
215 return;
218 ZERO_STRUCTP(ctr);
219 ctr->op = ctr->sub_op = ctr->ioctl = -1;
220 DLIST_ADD(ctxt->ops, ctr);
222 pcd->context = (void*)ctxt;
225 static void perfcount_test_add(struct smb_perfcount_data *pcd)
227 struct perfcount_test_context *ctxt =
228 (struct perfcount_test_context *)pcd->context;
229 struct perfcount_test_counter *ctr;
231 if (pcd->context == NULL)
232 return;
234 ctr = SMB_MALLOC_P(struct perfcount_test_counter);
235 if (!ctr) {
236 return;
239 DLIST_ADD(ctxt->ops, ctr);
243 static void perfcount_test_set_op(struct smb_perfcount_data *pcd, int op)
245 struct perfcount_test_context *ctxt =
246 (struct perfcount_test_context *)pcd->context;
248 if (pcd->context == NULL)
249 return;
251 ctxt->ops->op = op;
254 static void perfcount_test_set_subop(struct smb_perfcount_data *pcd, int sub_op)
256 struct perfcount_test_context *ctxt =
257 (struct perfcount_test_context *)pcd->context;
259 if (pcd->context == NULL)
260 return;
262 ctxt->ops->sub_op = sub_op;
265 static void perfcount_test_set_ioctl(struct smb_perfcount_data *pcd, int io_ctl)
267 struct perfcount_test_context *ctxt =
268 (struct perfcount_test_context *)pcd->context;
269 if (pcd->context == NULL)
270 return;
272 ctxt->ops->ioctl = io_ctl;
275 static void perfcount_test_set_msglen_in(struct smb_perfcount_data *pcd,
276 uint64_t bytes_in)
278 struct perfcount_test_context *ctxt =
279 (struct perfcount_test_context *)pcd->context;
280 if (pcd->context == NULL)
281 return;
283 ctxt->ops->bytes_in = bytes_in;
286 static void perfcount_test_set_msglen_out(struct smb_perfcount_data *pcd,
287 uint64_t bytes_out)
289 struct perfcount_test_context *ctxt =
290 (struct perfcount_test_context *)pcd->context;
292 if (pcd->context == NULL)
293 return;
295 ctxt->ops->bytes_out = bytes_out;
298 static void perfcount_test_copy_context(struct smb_perfcount_data *pcd,
299 struct smb_perfcount_data *new_pcd)
301 struct perfcount_test_context *ctxt =
302 (struct perfcount_test_context *)pcd->context;
303 struct perfcount_test_context *new_ctxt;
305 struct perfcount_test_counter *ctr;
306 struct perfcount_test_counter *new_ctr;
308 if (pcd->context == NULL)
309 return;
311 new_ctxt = SMB_MALLOC_P(struct perfcount_test_context);
312 if (!new_ctxt) {
313 return;
316 memcpy(new_ctxt, ctxt, sizeof(struct perfcount_test_context));
318 for (ctr = ctxt->ops; ctr != NULL; ctr = ctr->next) {
319 new_ctr = SMB_MALLOC_P(struct perfcount_test_counter);
320 if (!new_ctr) {
321 goto error;
324 memcpy(new_ctr, ctr, sizeof(struct perfcount_test_counter));
325 new_ctr->next = NULL;
326 new_ctr->prev = NULL;
327 DLIST_ADD(new_ctxt->ops, new_ctr);
330 new_pcd->context = new_ctxt;
331 return;
333 error:
335 for (ctr = new_ctxt->ops; ctr != NULL; ) {
336 new_ctr = ctr->next;
337 SAFE_FREE(ctr);
338 ctr = new_ctr;
341 SAFE_FREE(new_ctxt);
345 * For perf reasons, its best to use some global state
346 * when an operation is deferred, we need to alloc a copy.
348 static void perfcount_test_defer_op(struct smb_perfcount_data *pcd,
349 struct smb_perfcount_data *def_pcd)
351 /* we don't do anything special to deferred ops */
352 return;
355 static void perfcount_test_end(struct smb_perfcount_data *pcd)
357 struct perfcount_test_context *ctxt =
358 (struct perfcount_test_context *)pcd->context;
359 if (pcd->context == NULL)
360 return;
362 /* @bug - we don't store outbytes right for chained cmds */
363 perfcount_test_add_counters(ctxt);
364 perfcount_test_dump_counters();
365 pcd->context = NULL;
366 SAFE_FREE(ctxt);
370 static struct smb_perfcount_handlers perfcount_test_handlers = {
371 perfcount_test_start,
372 perfcount_test_add,
373 perfcount_test_set_op,
374 perfcount_test_set_subop,
375 perfcount_test_set_ioctl,
376 perfcount_test_set_msglen_in,
377 perfcount_test_set_msglen_out,
378 perfcount_test_copy_context,
379 perfcount_test_defer_op,
380 perfcount_test_end
383 NTSTATUS perfcount_test_init(void)
385 return smb_register_perfcounter(SMB_PERFCOUNTER_INTERFACE_VERSION,
386 "pc_test", &perfcount_test_handlers);