(root)/
glibc-2.38/
crypt/
speeds.c
       1  /*
       2   * This fcrypt/crypt speed testing program
       3   * is derived from one floating around in
       4   * the net. It's distributed along with
       5   * UFC-crypt but is not covered by any
       6   * licence.
       7   *
       8   * @(#)speeds.c	1.11 20 Aug 1996
       9   */
      10  
      11  #include <signal.h>
      12  #include <stdio.h>
      13  
      14  #ifndef SIGVTALRM
      15  /*
      16   * patch from chip@chinacat.unicom.com (Chip Rosenthal):
      17   * you may enable it if your system does not include
      18   * a setitimer() function. You'll have to ensure the
      19   * existence an environment variable: HZ giving how many
      20   * ticks goes per second.
      21   * If not existing in your default environment 50, 60
      22   * or even 100 may be the right value. Perhaps you should
      23   * then use 'time ./ufc 10000' instead of guessing.
      24   */
      25  #define NO_ITIMER
      26  #endif
      27  
      28  #ifdef NO_ITIMER
      29  #include <sys/types.h>
      30  #include <sys/times.h>
      31  #else
      32  #include <sys/time.h>
      33  #endif
      34  
      35  static int cnt;
      36  #ifdef NO_ITIMER
      37  char *hz;
      38  struct tms tstart, tfinish;
      39  #endif
      40  #define ITIME	10		/* Number of seconds to run test. */
      41  
      42  char *crypt(), *fcrypt();
      43  
      44  void
      45  Stop (void)
      46  {
      47      double elapsed;
      48  #ifdef NO_ITIMER
      49      (void) times(&tfinish);
      50      elapsed = ((tfinish.tms_utime + tfinish.tms_stime) -
      51  	(tstart.tms_utime + tstart.tms_stime)) / atoi(hz);
      52      printf("elapsed time = %d sec,  CPU time = %f sec\n", ITIME, elapsed);
      53  #else
      54      elapsed = ITIME;
      55  #endif
      56      printf ("Did %f %s()s per second.\n", ((float) cnt) / elapsed,
      57  #if defined(FCRYPT)
      58  	    "fcrypt"
      59  #else
      60  	    "crypt"
      61  #endif
      62      );
      63      exit (0);
      64  }
      65  
      66  /*
      67   * Silly rewrite of 'bzero'. I do so
      68   * because some machines don't have
      69   * bzero and some don't have memset.
      70   */
      71  
      72  static void clearmem(start, cnt)
      73    char *start;
      74    int cnt;
      75    { while(cnt--)
      76        *start++ = '\0';
      77    }
      78  
      79  main (void)
      80  {
      81     char *s;
      82  #ifdef NO_ITIMER
      83      extern char *getenv();
      84  #else
      85      struct itimerval itv;
      86  #endif
      87  
      88  #ifdef NO_ITIMER
      89      if ((hz = getenv("HZ")) == NULL) {
      90  	fprintf(stderr, "HZ environment parameter undefined\n");
      91  	exit(1);
      92      }
      93  #endif
      94  
      95  #ifdef FCRYPT
      96      printf("\n");
      97      printf("Warning: this version of the speed program may run slower when\n");
      98      printf("benchmarking UFC-crypt than previous versions. This is because it\n");
      99      printf("stresses the CPU hardware cache in order to get benchmark figures\n");
     100      printf("that corresponds closer to the performance that can be expected in\n");
     101      printf("a password cracker.\n\n");
     102  #endif
     103  
     104      printf ("Running %s for %d seconds of virtual time ...\n",
     105  #ifdef FCRYPT
     106      "UFC-crypt",
     107  #else
     108      "crypt(libc)",
     109  #endif
     110  	    ITIME);
     111  
     112  #ifdef FCRYPT
     113      init_des ();
     114  #endif
     115  
     116  #ifdef NO_ITIMER
     117      signal(SIGALRM, Stop);
     118      switch (fork()) {
     119      case -1:
     120  	perror("fork failed");
     121  	exit(1);
     122      case 0:
     123  	sleep(10);
     124  	kill(getppid(), SIGALRM);
     125  	exit(0);
     126      default:
     127  	(void) times(&tstart);
     128      }
     129  #else
     130      clearmem ((char*)&itv, (int)sizeof (itv));
     131      signal (SIGVTALRM, Stop);
     132      itv.it_value.tv_sec = ITIME;
     133      itv.it_value.tv_usec = 0;
     134      setitimer (ITIMER_VIRTUAL, &itv, NULL);
     135  #endif
     136  
     137  
     138      s = "fredred";
     139      for (cnt = 0;; cnt++)
     140      {
     141  #ifdef FCRYPT
     142  	s = fcrypt (s, "eek");
     143  #else
     144  	s = crypt (s, "eek");
     145  #endif
     146      }
     147  }
     148  
     149  
     150  
     151  
     152  
     153