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.
36 krb5_data piece = { .data = "hello", .length = 5 };
42 unsigned long my_ctime;
43 unsigned int my_cusec;
48 #define DEFAULT_N_THREADS 2
49 #define DEFAULT_INTERVAL 20 /* 5 * 60 */
52 int n_threads = DEFAULT_N_THREADS;
53 int interval = DEFAULT_INTERVAL;
56 static void wait_for_tick ()
62 } while (now == next);
65 static void try_one (struct tinfo *t)
69 char buf[100], buf2[100];
70 krb5_rcache my_rcache;
72 snprintf(buf, sizeof(buf), "host/all-in-one.mit.edu/%p@ATHENA.MIT.EDU",
75 r.client = (t->my_cusec & 7) + "abcdefgh@ATHENA.MIT.EDU";
77 if (t->now != t->my_ctime) {
78 if (t->my_ctime != 0) {
79 snprintf(buf2, sizeof(buf2), "%3d: %ld %5d\n", t->idx,
80 t->my_ctime, t->my_cusec);
87 r.ctime = t->my_ctime;
88 r.cusec = t->my_cusec;
90 err = krb5_get_server_rcache(ctx, &piece, &my_rcache);
92 const char *msg = krb5_get_error_message(ctx, err);
93 fprintf(stderr, "%s: %s while initializing replay cache\n", prog, msg);
94 krb5_free_error_message(ctx, msg);
99 err = krb5_rc_store(ctx, my_rcache, &r);
101 com_err(prog, err, "storing in replay cache");
105 krb5_rc_close(ctx, my_rcache);
108 static void *run_a_loop (void *x)
110 struct tinfo t = { 0 };
111 /* int chr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_"[(*(int*)x) % 27]; */
115 while (t.now != time(0))
118 while (t.now < end_time) {
127 /* printf("thread %u total %u\n", (unsigned) ((int *)x-ip), t.total);*/
132 static void usage(void)
134 fprintf (stderr, "usage: %s [ options ]\n", prog);
135 fprintf (stderr, "options:\n");
136 fprintf (stderr, "\t-1\tcreate one rcache handle for process\n");
137 fprintf (stderr, "\t-t N\tnumber of threads to create (default: %d)\n",
140 "\t-i N\tinterval to run test over, in seconds (default: %d)\n",
145 static const char optstring[] = "1t:i:";
147 static void process_options (int argc, char *argv[])
152 while ((c = getopt(argc, argv, optstring)) != -1) {
162 n_threads = atoi (optarg);
163 if (n_threads < 1 || n_threads > 10000)
167 interval = atoi (optarg);
168 if (interval < 2 || n_threads > 100000)
175 int main (int argc, char *argv[])
181 process_options (argc, argv);
182 err = krb5_init_context(&ctx);
184 com_err(prog, err, "initializing context");
189 * For consistency, run the tests without an existing replay
190 * cache. Since there isn't a way to ask the library for the
191 * pathname that would be used for the rcache, we create an rcache
192 * object and then destroy it.
194 err = krb5_get_server_rcache(ctx, &piece, &rcache);
196 const char *msg = krb5_get_error_message(ctx, err);
197 fprintf(stderr, "%s: %s while initializing replay cache\n", prog, msg);
198 krb5_free_error_message(ctx, msg);
201 err = krb5_rc_destroy(ctx, rcache);
203 const char *msg = krb5_get_error_message(ctx, err);
204 fprintf(stderr, "%s: %s while destroying old replay cache\n",
206 krb5_free_error_message(ctx, msg);
212 err = krb5_get_server_rcache(ctx, &piece, &rcache);
214 const char *msg = krb5_get_error_message(ctx, err);
215 fprintf(stderr, "%s: %s while initializing new replay cache\n",
217 krb5_free_error_message(ctx, msg);
222 ip = malloc(sizeof(int) * n_threads);
223 if (ip == 0 && n_threads > 0) {
227 for (i = 0; i < n_threads; i++)
231 end_time = time(0) + interval;
233 for (i = 0; i < n_threads; i++) {
234 pthread_t new_thread;
236 perr = pthread_create(&new_thread, 0, run_a_loop, &ip[i]);
239 perror("pthread_create");
243 while (time(0) < end_time + 1)
246 for (i = 0; i < n_threads; i++) {
248 printf("thread %d total %5d, about %.1f per second\n", i, ip[i],
249 ((double) ip[i])/interval);
251 printf("total %lu in %d seconds, avg ~%.1f/sec, ~%.1f/sec/thread\n",
253 ((double)sum)/interval, ((double)sum)/interval/n_threads);
257 krb5_rc_close(ctx, rcache);
258 krb5_free_context(ctx);