Change HZ in speed to rely on sysconf() if the clock tick is available
[openssl.git] / apps / speed.c
index 6bfe345f81d35690e758bf236f0e96fc237154b5..45df8aa91b3204665fbb5b848a690c8d482ecd6a 100644 (file)
@@ -1,4 +1,4 @@
-/* apps/speed.c */
+/* apps/speed.c -*- mode:C; c-file-style: "eay" -*- */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
@@ -81,9 +81,9 @@
 #include <openssl/crypto.h>
 #include <openssl/rand.h>
 #include <openssl/err.h>
-#include <openssl/engine.h>
 #include <openssl/evp.h>
 #include <openssl/objects.h>
+#include OPENSSL_UNISTD
 
 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(OPENSSL_SYS_MACOSX)
 # define USE_TOD
 
 /* The following if from times(3) man page.  It may need to be changed */
 #ifndef HZ
-# ifndef CLK_TCK
-#  ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
-#   define HZ  100.0
-#  else /* _BSD_CLK_TCK_ */
-#   define HZ ((double)_BSD_CLK_TCK_)
+# ifdef _SC_CLK_TCK
+#  define HZ ((double)sysconf(_SC_CLK_TCK))
+# else
+#  ifndef CLK_TCK
+#   ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
+#    define HZ 100.0
+#   else /* _BSD_CLK_TCK_ */
+#    define HZ ((double)_BSD_CLK_TCK_)
+#   endif
+#  else /* CLK_TCK */
+#   define HZ ((double)CLK_TCK)
 #  endif
-# else /* CLK_TCK */
-#  define HZ ((double)CLK_TCK)
 # endif
 #endif
 
 #define BUFSIZE        ((long)1024*8+1)
 int run=0;
 
+static char ftime_used = 0, times_used = 0, gettimeofday_used = 0, getrusage_used = 0;
+
 static double Time_F(int s, int usertime);
 static void print_message(const char *s,long num,int length);
 static void pkey_print_message(char *str,char *str2,long num,int bits,int sec);
@@ -230,6 +236,7 @@ static double Time_F(int s, int usertime)
            {
                static struct rusage tstart,tend;
 
+               getrusage_used = 1;
                if (s == START)
                        {
                        getrusage(RUSAGE_SELF,&tstart);
@@ -251,6 +258,7 @@ static double Time_F(int s, int usertime)
                static struct timeval tstart,tend;
                long i;
 
+               gettimeofday_used = 1;
                if (s == START)
                        {
                        gettimeofday(&tstart,NULL);
@@ -271,6 +279,7 @@ static double Time_F(int s, int usertime)
                {
                static struct tms tstart,tend;
 
+               times_used = 1;
                if (s == START)
                        {
                        times(&tstart);
@@ -292,6 +301,7 @@ static double Time_F(int s, int usertime)
                static struct timeb tstart,tend;
                long i;
 
+               ftime_used = 1;
                if (s == START)
                        {
                        ftime(&tstart);
@@ -478,7 +488,11 @@ int MAIN(int argc, char **argv)
        while (argc)
                {
                if      ((argc > 0) && (strcmp(*argv,"-elapsed") == 0))
+                       {
                        usertime = 0;
+                       j--;    /* Otherwise, -elapsed gets confused with
+                                  an algorithm. */
+                       }
                else if ((argc > 0) && (strcmp(*argv,"-evp") == 0))
                        {
                        argc--;
@@ -496,10 +510,13 @@ int MAIN(int argc, char **argv)
                                }
                        doit[D_EVP]=1;
                        }
-               else if(argc > 0 && !strcmp(*argv,"-decrypt"))
+               else if (argc > 0 && !strcmp(*argv,"-decrypt"))
+                       {
                        decrypt=1;
-               else
-               if      ((argc > 0) && (strcmp(*argv,"-engine") == 0))
+                       j--;    /* Otherwise, -elapsed gets confused with
+                                  an algorithm. */
+                       }
+               else if ((argc > 0) && (strcmp(*argv,"-engine") == 0))
                        {
                        argc--;
                        argv++;
@@ -571,7 +588,7 @@ int MAIN(int argc, char **argv)
 #ifndef RSA_NULL
                        if (strcmp(*argv,"openssl") == 0) 
                        {
-                       RSA_set_default_openssl_method(RSA_PKCS1_SSLeay());
+                       RSA_set_default_method(RSA_PKCS1_SSLeay());
                        j--;
                        }
                else
@@ -787,9 +804,9 @@ int MAIN(int argc, char **argv)
 #endif
 
 #ifndef OPENSSL_NO_DES
-       des_set_key_unchecked(&key,sch);
-       des_set_key_unchecked(&key2,sch2);
-       des_set_key_unchecked(&key3,sch3);
+       des_set_key_unchecked(&key,&sch);
+       des_set_key_unchecked(&key2,&sch2);
+       des_set_key_unchecked(&key3,&sch3);
 #endif
 #ifndef OPENSSL_NO_IDEA
        idea_set_encrypt_key(key16,&idea_ks);
@@ -977,7 +994,7 @@ int MAIN(int argc, char **argv)
                        print_message(names[D_MD5],c[D_MD5][j],lengths[j]);
                        Time_F(START,usertime);
                        for (count=0,run=1; COND(c[D_MD5][j]); count++)
-                               EVP_Digest(&(buf[0]),(unsigned long)lengths[j],&(md5[0]),NULL,EVP_md5());
+                               EVP_Digest(&(buf[0]),(unsigned long)lengths[j],&(md5[0]),NULL,EVP_get_digestbyname("md5"));
                        d=Time_F(STOP,usertime);
                        BIO_printf(bio_err,"%ld %s's in %.2fs\n",
                                count,names[D_MD5],d);
@@ -990,6 +1007,8 @@ int MAIN(int argc, char **argv)
        if (doit[D_HMAC])
                {
                HMAC_CTX hctx;
+
+               HMAC_CTX_init(&hctx);
                HMAC_Init(&hctx,(unsigned char *)"This is a key...",
                        16,EVP_md5());
 
@@ -1008,7 +1027,7 @@ int MAIN(int argc, char **argv)
                                count,names[D_HMAC],d);
                        results[D_HMAC][j]=((double)count)/d*lengths[j];
                        }
-               HMAC_cleanup(&hctx);
+               HMAC_CTX_cleanup(&hctx);
                }
 #endif
 #ifndef OPENSSL_NO_SHA
@@ -1068,11 +1087,11 @@ int MAIN(int argc, char **argv)
                        print_message(names[D_CBC_DES],c[D_CBC_DES][j],lengths[j]);
                        Time_F(START,usertime);
                        for (count=0,run=1; COND(c[D_CBC_DES][j]); count++)
-                               des_ncbc_encrypt(buf,buf,lengths[j],sch,
+                               des_ncbc_encrypt(buf,buf,lengths[j],&sch,
                                                 &iv,DES_ENCRYPT);
                        d=Time_F(STOP,usertime);
                        BIO_printf(bio_err,"%ld %s's in %.2fs\n",
-                               count,names[D_CBC_DES],d);
+                                  count,names[D_CBC_DES],d);
                        results[D_CBC_DES][j]=((double)count)/d*lengths[j];
                        }
                }
@@ -1085,7 +1104,7 @@ int MAIN(int argc, char **argv)
                        Time_F(START,usertime);
                        for (count=0,run=1; COND(c[D_EDE3_DES][j]); count++)
                                des_ede3_cbc_encrypt(buf,buf,lengths[j],
-                                                    sch,sch2,sch3,
+                                                    &sch,&sch2,&sch3,
                                                     &iv,DES_ENCRYPT);
                        d=Time_F(STOP,usertime);
                        BIO_printf(bio_err,"%ld %s's in %.2fs\n",
@@ -1415,6 +1434,33 @@ int MAIN(int argc, char **argv)
        printf("%s ",BF_options());
 #endif
        fprintf(stdout,"\n%s\n",SSLeay_version(SSLEAY_CFLAGS));
+       printf("available timing options: ");
+#ifdef TIMES
+       printf("TIMES ");
+#endif
+#ifdef TIMEB
+       printf("TIMEB ");
+#endif
+#ifdef USE_TOD
+       printf("USE_TOD ");
+#endif
+#ifdef HZ
+#define as_string(s) (#s)
+       printf("HZ=%g", HZ);
+# ifdef _SC_CLK_TCK
+       printf(" [sysconf value]");
+# endif
+#endif
+       printf("\n");
+       printf("timing function used: %s%s%s%s%s%s%s\n",
+               (ftime_used ? "ftime" : ""),
+               (ftime_used + times_used > 1 ? "," : ""),
+               (times_used ? "times" : ""),
+               (ftime_used + times_used + gettimeofday_used > 1 ? "," : ""),
+               (gettimeofday_used ? "gettimeofday" : ""),
+               (ftime_used + times_used + gettimeofday_used + getrusage_used > 1 ? "," : ""),
+               (getrusage_used ? "getrusage" : ""));
+
 
        if (pr_header)
                {