Imported Upstream version 1.17
[platform/upstream/krb5.git] / src / tests / threads / t_rcache.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/threads/t_rcache.c */
3 /*
4  * Copyright (C) 2006 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
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.
11  *
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.
25  */
26
27 #include "k5-int.h"
28 #include <com_err.h>
29 #include <krb5.h>
30 #include <pthread.h>
31
32 krb5_context ctx;
33 krb5_rcache rcache;
34 krb5_data piece = { .data = "hello", .length = 5 };
35 time_t end_time;
36 const char *prog;
37
38 struct tinfo {
39     time_t now;
40     unsigned long my_ctime;
41     unsigned int my_cusec;
42     unsigned int total;
43     int idx;
44 };
45
46 #define DEFAULT_N_THREADS   2
47 #define DEFAULT_INTERVAL   20 /* 5 * 60 */
48
49 int init_once = 0;
50 int n_threads = DEFAULT_N_THREADS;
51 int interval = DEFAULT_INTERVAL;
52 int *ip;
53
54 static void wait_for_tick ()
55 {
56     time_t now, next;
57     now = time(0);
58     do {
59         next = time(0);
60     } while (now == next);
61 }
62
63 static void try_one (struct tinfo *t)
64 {
65     krb5_donot_replay r;
66     krb5_error_code err;
67     char buf[100], buf2[100];
68     krb5_rcache my_rcache;
69
70     snprintf(buf, sizeof(buf), "host/all-in-one.mit.edu/%p@ATHENA.MIT.EDU",
71              buf);
72     r.server = buf;
73     r.client = (t->my_cusec & 7) + "abcdefgh@ATHENA.MIT.EDU";
74     r.msghash = NULL;
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);
79             printf("%s", buf2);
80         }
81         t->my_ctime = t->now;
82         t->my_cusec = 1;
83     } else
84         t->my_cusec++;
85     r.ctime = t->my_ctime;
86     r.cusec = t->my_cusec;
87     if (!init_once) {
88         err = krb5_get_server_rcache(ctx, &piece, &my_rcache);
89         if (err) {
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);
93             exit(1);
94         }
95     } else
96         my_rcache = rcache;
97     err = krb5_rc_store(ctx, my_rcache, &r);
98     if (err) {
99         com_err(prog, err, "storing in replay cache");
100         exit(1);
101     }
102     if (!init_once)
103         krb5_rc_close(ctx, my_rcache);
104 }
105
106 static void *run_a_loop (void *x)
107 {
108     struct tinfo t = { 0 };
109
110     t.now = time(0);
111     t.idx = *(int *)x;
112     while (t.now != time(0))
113         ;
114     t.now = time(0);
115     while (t.now < end_time) {
116         t.now = time(0);
117         try_one(&t);
118         t.total++;
119     }
120     *(int*)x = t.total;
121     return 0;
122 }
123
124 static void usage(void)
125 {
126     fprintf (stderr, "usage: %s [ options ]\n", prog);
127     fprintf (stderr, "options:\n");
128     fprintf (stderr, "\t-1\tcreate one rcache handle for process\n");
129     fprintf (stderr, "\t-t N\tnumber of threads to create (default: %d)\n",
130              DEFAULT_N_THREADS);
131     fprintf (stderr,
132              "\t-i N\tinterval to run test over, in seconds (default: %d)\n",
133              DEFAULT_INTERVAL);
134     exit(1);
135 }
136
137 static const char optstring[] = "1t:i:";
138
139 static void process_options (int argc, char *argv[])
140 {
141     int c;
142
143     prog = argv[0];
144     while ((c = getopt(argc, argv, optstring)) != -1) {
145         switch (c) {
146         case '?':
147         case ':':
148         default:
149             usage ();
150         case '1':
151             init_once = 1;
152             break;
153         case 't':
154             n_threads = atoi (optarg);
155             if (n_threads < 1 || n_threads > 10000)
156                 usage ();
157             break;
158         case 'i':
159             interval = atoi (optarg);
160             if (interval < 2 || n_threads > 100000)
161                 usage ();
162             break;
163         }
164     }
165 }
166
167 int main (int argc, char *argv[])
168 {
169     krb5_error_code err;
170     int i;
171     unsigned long sum;
172
173     process_options (argc, argv);
174     err = krb5_init_context(&ctx);
175     if (err) {
176         com_err(prog, err, "initializing context");
177         return 1;
178     }
179
180     /*
181      * For consistency, run the tests without an existing replay
182      * cache.  Since there isn't a way to ask the library for the
183      * pathname that would be used for the rcache, we create an rcache
184      * object and then destroy it.
185      */
186     err = krb5_get_server_rcache(ctx, &piece, &rcache);
187     if (err) {
188         const char *msg = krb5_get_error_message(ctx, err);
189         fprintf(stderr, "%s: %s while initializing replay cache\n", prog, msg);
190         krb5_free_error_message(ctx, msg);
191         return 1;
192     }
193     err = krb5_rc_destroy(ctx, rcache);
194     if (err) {
195         const char *msg = krb5_get_error_message(ctx, err);
196         fprintf(stderr, "%s: %s while destroying old replay cache\n",
197                 prog, msg);
198         krb5_free_error_message(ctx, msg);
199         return 1;
200     }
201     rcache = NULL;
202
203     if (init_once) {
204         err = krb5_get_server_rcache(ctx, &piece, &rcache);
205         if (err) {
206             const char *msg = krb5_get_error_message(ctx, err);
207             fprintf(stderr, "%s: %s while initializing new replay cache\n",
208                     prog, msg);
209             krb5_free_error_message(ctx, msg);
210             return 1;
211         }
212     }
213
214     ip = malloc(sizeof(int) * n_threads);
215     if (ip == 0 && n_threads > 0) {
216         perror("malloc");
217         exit(1);
218     }
219     for (i = 0; i < n_threads; i++)
220         ip[i] = i;
221
222     wait_for_tick ();
223     end_time = time(0) + interval;
224
225     for (i = 0; i < n_threads; i++) {
226         pthread_t new_thread;
227         int perr;
228         perr = pthread_create(&new_thread, 0, run_a_loop, &ip[i]);
229         if (perr) {
230             errno = perr;
231             perror("pthread_create");
232             exit(1);
233         }
234     }
235     while (time(0) < end_time + 1)
236         sleep(1);
237     sum = 0;
238     for (i = 0; i < n_threads; i++) {
239         sum += ip[i];
240         printf("thread %d total %5d, about %.1f per second\n", i, ip[i],
241                ((double) ip[i])/interval);
242     }
243     printf("total %lu in %d seconds, avg ~%.1f/sec, ~%.1f/sec/thread\n",
244            sum, interval,
245            ((double)sum)/interval, ((double)sum)/interval/n_threads);
246     free(ip);
247
248     if (init_once)
249         krb5_rc_close(ctx, rcache);
250     krb5_free_context(ctx);
251     return 0;
252 }