PageRenderTime 26ms CodeModel.GetById 10ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/security/nss/cmd/libpkix/perf/libpkix_buildthreads.c

http://github.com/zpao/v8monkey
C | 382 lines | 241 code | 70 blank | 71 comment | 11 complexity | 75c44383d59046b3c183437d991576ec MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is the PKIX-C library.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Sun Microsystems, Inc.
 18 * Portions created by the Initial Developer are
 19 * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *   Sun Microsystems, Inc.
 23 *
 24 * Alternatively, the contents of this file may be used under the terms of
 25 * either the GNU General Public License Version 2 or later (the "GPL"), or
 26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 27 * in which case the provisions of the GPL or the LGPL are applicable instead
 28 * of those above. If you wish to allow use of your version of this file only
 29 * under the terms of either the GPL or the LGPL, and not to allow others to
 30 * use your version of this file under the terms of the MPL, indicate your
 31 * decision by deleting the provisions above and replace them with the notice
 32 * and other provisions required by the GPL or the LGPL. If you do not delete
 33 * the provisions above, a recipient may use your version of this file under
 34 * the terms of any one of the MPL, the GPL or the LGPL.
 35 *
 36 * ***** END LICENSE BLOCK ***** */
 37/*
 38 * libpkixBuildThreads.c
 39 *
 40 * libpkix Builder Performance Evaluation application (multi-threaded)
 41 *
 42 */
 43
 44#include <stdio.h>
 45#include <string.h>
 46
 47#include "secutil.h"
 48
 49#include "nspr.h"
 50#include "prtypes.h"
 51#include "prtime.h"
 52#include "prlong.h"
 53
 54#include "pk11func.h"
 55#include "secasn1.h"
 56#include "cert.h"
 57#include "cryptohi.h"
 58#include "secoid.h"
 59#include "certdb.h"
 60#include "nss.h"
 61
 62#include "pkix.h"
 63#include "pkix_tools.h"
 64#include "pkix_pl_cert.h"
 65
 66#include "testutil.h"
 67#include "testutil_nss.h"
 68
 69static void *plContext = NULL;
 70
 71#undef pkixTempResult
 72#define PERF_DECREF(obj) \
 73        { \
 74                PKIX_Error *pkixTempResult = NULL; \
 75                if (obj){ \
 76                        pkixTempResult = PKIX_PL_Object_DecRef \
 77                        ((PKIX_PL_Object *)(obj), plContext); \
 78                        obj = NULL; \
 79                } \
 80        }
 81
 82static void finish(char* message, int code);
 83
 84typedef struct ThreadDataStr tData;
 85
 86struct ThreadDataStr {
 87    CERTCertificate* anchor;
 88    char* eecertName;
 89    PRIntervalTime duration;
 90    CERTCertDBHandle *handle;
 91    PRUint32 iterations;
 92};
 93
 94#define PKIX_LOGGER_ON 1
 95
 96#ifdef PKIX_LOGGER_ON
 97
 98char *logLevels[] = {
 99        "None",
100        "Fatal Error",
101        "Error",
102        "Warning",
103        "Debug",
104        "Trace"
105};
106
107static PKIX_Error *loggerCallback(
108        PKIX_Logger *logger,
109        PKIX_PL_String *message,
110        PKIX_UInt32 logLevel,
111        PKIX_ERRORCLASS logComponent,
112        void *plContext)
113{
114        char *msg = NULL;
115        static int callCount = 0;
116
117        msg = PKIX_String2ASCII(message, plContext);
118        printf("Logging %s (%s): %s\n",
119                logLevels[logLevel],
120                PKIX_ERRORCLASSNAMES[logComponent],
121                msg);
122        PR_Free((void *)msg);
123
124        return(NULL);
125}
126
127#endif /* PKIX_LOGGER_ON */
128
129static void ThreadEntry(void* data)
130{
131        tData* tdata = (tData*) data;
132        PRIntervalTime duration = tdata->duration;
133        PRIntervalTime start = PR_IntervalNow();
134
135        PKIX_List *anchors = NULL;
136        PKIX_ProcessingParams *procParams = NULL;
137        PKIX_BuildResult *buildResult = NULL;
138        CERTCertificate* nsseecert;
139        PKIX_PL_Cert *eeCert = NULL;
140        PKIX_CertStore *certStore = NULL;
141        PKIX_List *certStores = NULL;
142        PKIX_ComCertSelParams *certSelParams = NULL;
143        PKIX_CertSelector *certSelector = NULL;
144        PKIX_PL_Date *nowDate = NULL;
145        void *state = NULL; /* only relevant with non-blocking I/O */
146        void *nbioContext = NULL; /* only relevant with non-blocking I/O */
147
148        PR_ASSERT(duration);
149        if (!duration){
150                return;
151        }
152
153        do {
154
155                /* libpkix code */
156
157                /* keep more update time, testing cache */
158                PKIX_PL_Date_Create_UTCTime(NULL, &nowDate, plContext);
159
160                /* CertUsage is 0x10 and no NSS arena */
161                /* We haven't determined how we obtain the value of wincx */
162
163                nsseecert = CERT_FindCertByNicknameOrEmailAddr(tdata->handle,
164                        tdata->eecertName);
165                if (!nsseecert) finish("Unable to find eecert.\n", 1);
166
167                pkix_pl_Cert_CreateWithNSSCert
168                        (nsseecert, &eeCert, plContext);
169
170                PKIX_List_Create(&anchors, plContext);
171
172        /*
173         * This code is retired.
174         *      pkix_pl_Cert_CreateWithNSSCert
175         *              (tdata->anchor, &anchorCert, NULL);
176         *      PKIX_TrustAnchor_CreateWithCert(anchorCert, &anchor, NULL);
177         *      PKIX_List_AppendItem(anchors, (PKIX_PL_Object *)anchor, NULL);
178         */
179
180                PKIX_ProcessingParams_Create(anchors, &procParams, plContext);
181
182                PKIX_ProcessingParams_SetRevocationEnabled
183                        (procParams, PKIX_TRUE, plContext);
184
185                PKIX_ProcessingParams_SetDate
186                        (procParams, nowDate, plContext);
187
188                /* create CertSelector with target certificate in params */
189
190                PKIX_ComCertSelParams_Create(&certSelParams, plContext);
191
192                PKIX_ComCertSelParams_SetCertificate
193                        (certSelParams, eeCert, plContext);
194
195                PKIX_CertSelector_Create
196                        (NULL, NULL, &certSelector, plContext);
197
198                PKIX_CertSelector_SetCommonCertSelectorParams
199                        (certSelector, certSelParams, plContext);
200
201                PKIX_ProcessingParams_SetTargetCertConstraints
202                        (procParams, certSelector, plContext);
203
204                PKIX_PL_Pk11CertStore_Create(&certStore, plContext);
205
206                PKIX_List_Create(&certStores, plContext);
207                PKIX_List_AppendItem
208                        (certStores, (PKIX_PL_Object *)certStore, plContext);
209                PKIX_ProcessingParams_SetCertStores
210                        (procParams, certStores, plContext);
211
212                PKIX_BuildChain
213                        (procParams,
214                        &nbioContext,
215                        &state,
216                        &buildResult,
217                        NULL,
218                        plContext);
219
220                /*
221                 * As long as we use only CertStores with blocking I/O, we
222                 * know we must be done at this point.
223                 */
224
225                if (!buildResult){
226                        (void) fprintf(stderr, "libpkix BuildChain failed.\n");
227                        PORT_Assert(0);
228                        return;
229                }
230
231                tdata->iterations ++;
232
233                PERF_DECREF(nowDate);
234                PERF_DECREF(anchors);
235                PERF_DECREF(procParams);
236                PERF_DECREF(buildResult);
237                PERF_DECREF(certStore);
238                PERF_DECREF(certStores);
239                PERF_DECREF(certSelParams);
240                PERF_DECREF(certSelector);
241                PERF_DECREF(eeCert);
242
243        } while ((PR_IntervalNow() - start) < duration);
244
245
246}
247
248static void
249Test(
250        CERTCertificate* anchor,
251        char* eecertName,
252        PRIntervalTime duration,
253        CERTCertDBHandle *handle,
254        PRUint32 threads)
255{
256        tData data;
257        tData** alldata;
258        PRIntervalTime starttime, endtime, elapsed;
259        PRUint32 msecs;
260        float total = 0;
261        PRThread** pthreads = NULL;
262        PRUint32 i = 0;
263
264        data.duration = duration;
265        data.anchor = anchor;
266        data.eecertName = eecertName;
267        data.handle = handle;
268
269        data.iterations = 0;
270
271        starttime = PR_IntervalNow();
272        pthreads = (PRThread**)PR_Malloc(threads*sizeof (PRThread*));
273        alldata = (tData**)PR_Malloc(threads*sizeof (tData*));
274        for (i = 0; i < threads; i++){
275                alldata[i] = (tData*)PR_Malloc(sizeof (tData));
276                *alldata[i] = data;
277                pthreads[i] =
278                        PR_CreateThread(PR_USER_THREAD,
279                                        ThreadEntry,
280                                        (void*) alldata[i],
281                                        PR_PRIORITY_NORMAL,
282                                        PR_GLOBAL_THREAD,
283                                        PR_JOINABLE_THREAD,
284                                        0);
285        }
286
287        for (i = 0; i < threads; i++) {
288                tData* args = alldata[i];
289                PR_JoinThread(pthreads[i]);
290                total += args->iterations;
291                PR_Free((void*)args);
292        }
293
294        PR_Free((void*) pthreads);
295        PR_Free((void*) alldata);
296        endtime = PR_IntervalNow();
297
298        endtime = PR_IntervalNow();
299        elapsed = endtime - starttime;
300        msecs = PR_IntervalToMilliseconds(elapsed);
301        total /= msecs;
302        total *= 1000;
303        (void) fprintf(stdout, "%f operations per second.\n", total);
304}
305
306
307static void finish(char* message, int code)
308{
309        (void) printf(message);
310        exit(code);
311}
312
313static void usage(char* progname)
314{
315        (void) printf("Usage : %s <-d certStoreDirectory> <duration> <threads> "
316                      "<anchorNickname> <eecertNickname>\n\n", progname);
317        finish("", 0);
318}
319
320int
321libpkix_buildthreads(int argc, char** argv)
322{
323        CERTCertDBHandle *handle = NULL;
324        CERTCertificate* eecert = NULL;
325        PRIntervalTime duration = PR_SecondsToInterval(1);
326        PRUint32 threads = 1;
327        PKIX_UInt32 actualMinorVersion;
328        PKIX_UInt32 j = 0;
329        PKIX_Logger *logger = NULL;
330        void *wincx = NULL;
331
332        /* if (argc != 5) -- when TrustAnchor used to be on command line */
333        if (argc != 4)
334                {
335                        usage(argv[0]);
336                }
337        if (atoi(argv[1]) > 0)
338                {
339                        duration = PR_SecondsToInterval(atoi(argv[1]));
340                }
341        if (atoi(argv[2]) > 0)
342                {
343                        threads = atoi(argv[2]);
344                }
345
346        PKIX_PL_NssContext_Create(certificateUsageEmailSigner, PKIX_FALSE,
347                                  NULL, &plContext);
348
349        handle = CERT_GetDefaultCertDB();
350        PR_ASSERT(handle);
351
352#ifdef PKIX_LOGGER_ON
353
354        /* set logger to log trace and up */
355        PKIX_SetLoggers(NULL, plContext);
356        PKIX_Logger_Create(loggerCallback, NULL, &logger, plContext);
357        PKIX_Logger_SetMaxLoggingLevel
358                (logger, PKIX_LOGGER_LEVEL_WARNING, plContext);
359        PKIX_AddLogger(logger, plContext);
360
361#endif /* PKIX_LOGGER_ON */
362
363        /*
364         * This code is retired
365         *      anchor = CERT_FindCertByNicknameOrEmailAddr(handle, argv[3]);
366         *      if (!anchor) finish("Unable to find anchor.\n", 1);
367         *
368         *      eecert = CERT_FindCertByNicknameOrEmailAddr(handle, argv[4]);
369     
370         *      if (!eecert) finish("Unable to find eecert.\n", 1);
371         *
372         *      Test(anchor, eecert, duration, threads);
373         */
374
375        Test(NULL, argv[3], duration, handle, threads);
376
377        PERF_DECREF(logger);
378
379        PKIX_Shutdown(plContext);
380
381        return (0);
382}