1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/threads/t_rcache.c */
4 * Copyright (C) 2006 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
34 krb5_data piece = { .data = "hello", .length = 5 };
40 unsigned long my_ctime;
41 unsigned int my_cusec;
46 #define DEFAULT_N_THREADS 2
47 #define DEFAULT_INTERVAL 20 /* 5 * 60 */
50 int n_threads = DEFAULT_N_THREADS;
51 int interval = DEFAULT_INTERVAL;
54 static void wait_for_tick ()
60 } while (now == next);
63 static void try_one (struct tinfo *t)
67 char buf[100], buf2[100];
68 krb5_rcache my_rcache;
70 snprintf(buf, sizeof(buf), "host/all-in-one.mit.edu/%p@ATHENA.MIT.EDU",
73 r.client = (t->my_cusec & 7) + "abcdefgh@ATHENA.MIT.EDU";
75 if (t->now != t->my_ctime) {
76 if (t->my_ctime != 0) {
77 snprintf(buf2, sizeof(buf2), "%3d: %ld %5d\n", t->idx,
78 t->my_ctime, t->my_cusec);
85 r.ctime = t->my_ctime;
86 r.cusec = t->my_cusec;
88 err = krb5_get_server_rcache(ctx, &piece, &my_rcache);
90 const char *msg = krb5_get_error_message(ctx, err);
91 fprintf(stderr, "%s: %s while initializing replay cache\n", prog, msg);
92 krb5_free_error_message(ctx, msg);
97 err = krb5_rc_store(ctx, my_rcache, &r);
99 com_err(prog, err, "storing in replay cache");
103 krb5_rc_close(ctx, my_rcache);
106 static void *run_a_loop (void *x)
108 struct tinfo t = { 0 };
109 /* int chr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_"[(*(int*)x) % 27]; */
113 while (t.now != time(0))
116 while (t.now < end_time) {
125 /* printf("thread %u total %u\n", (unsigned) ((int *)x-ip), t.total);*/
130 static void usage(void)
132 fprintf (stderr, "usage: %s [ options ]\n", prog);
133 fprintf (stderr, "options:\n");
134 fprintf (stderr, "\t-1\tcreate one rcache handle for process\n");
135 fprintf (stderr, "\t-t N\tnumber of threads to create (default: %d)\n",
138 "\t-i N\tinterval to run test over, in seconds (default: %d)\n",
143 static const char optstring[] = "1t:i:";
145 static void process_options (int argc, char *argv[])
150 while ((c = getopt(argc, argv, optstring)) != -1) {
160 n_threads = atoi (optarg);
161 if (n_threads < 1 || n_threads > 10000)
165 interval = atoi (optarg);
166 if (interval < 2 || n_threads > 100000)
173 int main (int argc, char *argv[])
179 process_options (argc, argv);
180 err = krb5_init_context(&ctx);
182 com_err(prog, err, "initializing context");
187 * For consistency, run the tests without an existing replay
188 * cache. Since there isn't a way to ask the library for the
189 * pathname that would be used for the rcache, we create an rcache
190 * object and then destroy it.
192 err = krb5_get_server_rcache(ctx, &piece, &rcache);
194 const char *msg = krb5_get_error_message(ctx, err);
195 fprintf(stderr, "%s: %s while initializing replay cache\n", prog, msg);
196 krb5_free_error_message(ctx, msg);
199 err = krb5_rc_destroy(ctx, rcache);
201 const char *msg = krb5_get_error_message(ctx, err);
202 fprintf(stderr, "%s: %s while destroying old replay cache\n",
204 krb5_free_error_message(ctx, msg);
210 err = krb5_get_server_rcache(ctx, &piece, &rcache);
212 const char *msg = krb5_get_error_message(ctx, err);
213 fprintf(stderr, "%s: %s while initializing new replay cache\n",
215 krb5_free_error_message(ctx, msg);
220 ip = malloc(sizeof(int) * n_threads);
221 if (ip == 0 && n_threads > 0) {
225 for (i = 0; i < n_threads; i++)
229 end_time = time(0) + interval;
231 for (i = 0; i < n_threads; i++) {
232 pthread_t new_thread;
234 perr = pthread_create(&new_thread, 0, run_a_loop, &ip[i]);
237 perror("pthread_create");
241 while (time(0) < end_time + 1)
244 for (i = 0; i < n_threads; i++) {
246 printf("thread %d total %5d, about %.1f per second\n", i, ip[i],
247 ((double) ip[i])/interval);
249 printf("total %lu in %d seconds, avg ~%.1f/sec, ~%.1f/sec/thread\n",
251 ((double)sum)/interval, ((double)sum)/interval/n_threads);
255 krb5_rc_close(ctx, rcache);
256 krb5_free_context(ctx);