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/>.
22 #include "smbd/smbd.h"
24 #define PARM_PC_TEST_TYPE "pc_test"
25 #define PARM_DUMPON_COUNT "count"
26 #define PARM_DUMPON_COUNT_DEFAULT 50
28 struct perfcount_test_identity
{
34 struct perfcount_test_counter
{
42 struct perfcount_test_counter
*next
;
43 struct perfcount_test_counter
*prev
;
46 struct perfcount_test_context
{
49 struct perfcount_test_identity
*id
;
50 struct perfcount_test_counter
*ops
;
54 struct perfcount_test_counter
*g_list
[MAX_OP
];
58 /* determine frequency of dumping results */
61 static void perfcount_test_add_counters(struct perfcount_test_context
*ctxt
)
63 struct perfcount_test_counter
*head
;
64 struct perfcount_test_counter
*ptc
;
65 struct perfcount_test_counter
*tmp
;
68 for (ptc
= ctxt
->ops
; ptc
!= NULL
; ) {
75 for (head
= g_list
[ptc
->op
]; head
!= NULL
; head
= head
->next
) {
76 if ((ptc
->sub_op
== head
->sub_op
) &&
77 (ptc
->ioctl
== head
->ioctl
)) {
78 head
->bytes_in
+= ptc
->bytes_in
;
79 head
->bytes_out
+= ptc
->bytes_out
;
82 DLIST_REMOVE(ctxt
->ops
, ptc
);
90 /* not in global tracking list - add it */
93 DLIST_REMOVE(ctxt
->ops
, ptc
);
95 DLIST_ADD(g_list
[ptc
->op
], ptc
);
104 static void perfcount_test_dump_id(struct perfcount_test_identity
*id
, int lvl
)
109 DEBUG(lvl
,("uid - %d\n", id
->uid
));
110 DEBUG(lvl
,("user - %s\n", id
->user
));
111 DEBUG(lvl
,("domain - %s\n", id
->domain
));
116 static const char *trans_subop_table
[] = {
117 "unknown", "trans:create", "trans:ioctl", "trans:set sd",
118 "trans:change notify", "trans: rename", "trans:get sd",
119 "trans:get quota", "trans:set quota"
122 static const char *trans2_subop_table
[] = {
123 "trans2:open", "trans2:find first", "trans2:find next",
124 "trans2:q fsinfo", "trans2:set fsinfo", "trans2:q path info",
125 "trans2:set pathinfo", "trans2:fs ctl", "trans2: io ctl",
126 "trans2:find notify first", "trans2:find notify next",
127 "trans2:mkdir", "trans2:sess setup", "trans2:get dfs referral",
128 "trans2:report dfs inconsistent"
131 static const char *smb_subop_name(int op
, int subop
)
135 if (subop
> sizeof(trans_subop_table
) /
136 sizeof(trans_subop_table
[0])) {
139 return trans_subop_table
[subop
];
140 } else if (op
== 0x32) {
141 if (subop
> sizeof(trans2_subop_table
) /
142 sizeof(trans2_subop_table
[0])) {
145 return trans2_subop_table
[subop
];
151 static void perfcount_test_dump_counter(struct perfcount_test_counter
*ptc
,
154 DEBUG(lvl
, ("OP: %s\n", smb_fn_name(ptc
->op
)));
155 if (ptc
->sub_op
> 0) {
156 DEBUG(lvl
, ("SUBOP: %s\n",
157 smb_subop_name(ptc
->op
, ptc
->sub_op
)));
160 if (ptc
->ioctl
> 0) {
161 DEBUG(lvl
, ("IOCTL: %d\n", ptc
->ioctl
));
164 DEBUG(lvl
, ("Count: %d\n\n", ptc
->count
));
167 static void perfcount_test_dump_counters(void)
170 struct perfcount_test_counter
*head
;
172 count_mod
= lp_parm_int(0, PARM_PC_TEST_TYPE
, PARM_DUMPON_COUNT
,
173 PARM_DUMPON_COUNT_DEFAULT
);
175 if ((count
++ % count_mod
) != 0)
178 DEBUG(0,("##### Dumping Performance Counters #####\n"));
180 for (i
=0; i
< MAX_OP
; i
++) {
181 struct perfcount_test_counter
*next
;
182 for (head
= g_list
[i
]; head
!= NULL
; head
= next
) {
184 perfcount_test_dump_counter(head
, 0);
192 static void perfcount_test_start(struct smb_perfcount_data
*pcd
)
194 struct perfcount_test_context
*ctxt
;
195 struct perfcount_test_counter
*ctr
;
197 * there shouldn't already be a context here - if so,
198 * there's an unbalanced call to start / end.
201 DEBUG(0,("perfcount_test_start - starting "
202 "initialized context - %p\n", pcd
));
206 ctxt
= SMB_MALLOC_P(struct perfcount_test_context
);
212 /* create 'default' context */
213 ctr
= SMB_MALLOC_P(struct perfcount_test_counter
);
220 ctr
->op
= ctr
->sub_op
= ctr
->ioctl
= -1;
221 DLIST_ADD(ctxt
->ops
, ctr
);
223 pcd
->context
= (void*)ctxt
;
226 static void perfcount_test_add(struct smb_perfcount_data
*pcd
)
228 struct perfcount_test_context
*ctxt
=
229 (struct perfcount_test_context
*)pcd
->context
;
230 struct perfcount_test_counter
*ctr
;
232 if (pcd
->context
== NULL
)
235 ctr
= SMB_MALLOC_P(struct perfcount_test_counter
);
240 DLIST_ADD(ctxt
->ops
, ctr
);
244 static void perfcount_test_set_op(struct smb_perfcount_data
*pcd
, int op
)
246 struct perfcount_test_context
*ctxt
=
247 (struct perfcount_test_context
*)pcd
->context
;
249 if (pcd
->context
== NULL
)
255 static void perfcount_test_set_subop(struct smb_perfcount_data
*pcd
, int sub_op
)
257 struct perfcount_test_context
*ctxt
=
258 (struct perfcount_test_context
*)pcd
->context
;
260 if (pcd
->context
== NULL
)
263 ctxt
->ops
->sub_op
= sub_op
;
266 static void perfcount_test_set_ioctl(struct smb_perfcount_data
*pcd
, int io_ctl
)
268 struct perfcount_test_context
*ctxt
=
269 (struct perfcount_test_context
*)pcd
->context
;
270 if (pcd
->context
== NULL
)
273 ctxt
->ops
->ioctl
= io_ctl
;
276 static void perfcount_test_set_msglen_in(struct smb_perfcount_data
*pcd
,
279 struct perfcount_test_context
*ctxt
=
280 (struct perfcount_test_context
*)pcd
->context
;
281 if (pcd
->context
== NULL
)
284 ctxt
->ops
->bytes_in
= bytes_in
;
287 static void perfcount_test_set_msglen_out(struct smb_perfcount_data
*pcd
,
290 struct perfcount_test_context
*ctxt
=
291 (struct perfcount_test_context
*)pcd
->context
;
293 if (pcd
->context
== NULL
)
296 ctxt
->ops
->bytes_out
= bytes_out
;
299 static void perfcount_test_copy_context(struct smb_perfcount_data
*pcd
,
300 struct smb_perfcount_data
*new_pcd
)
302 struct perfcount_test_context
*ctxt
=
303 (struct perfcount_test_context
*)pcd
->context
;
304 struct perfcount_test_context
*new_ctxt
;
306 struct perfcount_test_counter
*ctr
;
307 struct perfcount_test_counter
*new_ctr
;
309 if (pcd
->context
== NULL
)
312 new_ctxt
= SMB_MALLOC_P(struct perfcount_test_context
);
317 memcpy(new_ctxt
, ctxt
, sizeof(struct perfcount_test_context
));
319 for (ctr
= ctxt
->ops
; ctr
!= NULL
; ctr
= ctr
->next
) {
320 new_ctr
= SMB_MALLOC_P(struct perfcount_test_counter
);
325 memcpy(new_ctr
, ctr
, sizeof(struct perfcount_test_counter
));
326 new_ctr
->next
= NULL
;
327 new_ctr
->prev
= NULL
;
328 DLIST_ADD(new_ctxt
->ops
, new_ctr
);
331 new_pcd
->context
= new_ctxt
;
336 for (ctr
= new_ctxt
->ops
; ctr
!= NULL
; ) {
346 * For perf reasons, its best to use some global state
347 * when an operation is deferred, we need to alloc a copy.
349 static void perfcount_test_defer_op(struct smb_perfcount_data
*pcd
,
350 struct smb_perfcount_data
*def_pcd
)
352 /* we don't do anything special to deferred ops */
356 static void perfcount_test_end(struct smb_perfcount_data
*pcd
)
358 struct perfcount_test_context
*ctxt
=
359 (struct perfcount_test_context
*)pcd
->context
;
360 if (pcd
->context
== NULL
)
363 /* @bug - we don't store outbytes right for chained cmds */
364 perfcount_test_add_counters(ctxt
);
365 perfcount_test_dump_counters();
371 static struct smb_perfcount_handlers perfcount_test_handlers
= {
372 perfcount_test_start
,
374 perfcount_test_set_op
,
375 perfcount_test_set_subop
,
376 perfcount_test_set_ioctl
,
377 perfcount_test_set_msglen_in
,
378 perfcount_test_set_msglen_out
,
379 perfcount_test_copy_context
,
380 perfcount_test_defer_op
,
384 NTSTATUS
samba_init_module(void)
386 return smb_register_perfcounter(SMB_PERFCOUNTER_INTERFACE_VERSION
,
387 "pc_test", &perfcount_test_handlers
);