simmer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
  3. * All rights reserved.
  4. * This component and the accompanying materials are made available
  5. * under the terms of the License "Eclipse Public License v1.0"
  6. * which accompanies this distribution, and is available
  7. * at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. *
  9. * Initial Contributors:
  10. * Nokia Corporation - initial contribution.
  11. *
  12. * Contributors:
  13. *
  14. * Description:
  15. * This program attempts to simulate the activity of a compiler. It can
  16. * then be used to test the scalability of the build system and measure
  17. * the contribution of various factors to scaling.
  18. */
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <stdarg.h>
  27. #include "../config.h"
  28. #include "log.h"
  29. #ifdef HAS_MSVCRT
  30. /* Make all output handling binary */
  31. unsigned int _CRT_fmode = _O_BINARY;
  32. #endif
  33. double getseconds(void)
  34. {
  35. struct timeval tp;
  36. gettimeofday(&tp, NULL);
  37. return (double)tp.tv_sec + ((double)tp.tv_usec)/1000000.0L;
  38. }
  39. void talon_setenv(char name[], char val[])
  40. {
  41. #if defined(HAS_GETENVIRONMENTVARIABLE)
  42. SetEnvironmentVariableA(name,val);
  43. #elif defined(HAS_GETENV)
  44. setenv(name,val, 1);
  45. #else
  46. # error "Need a function for setting environment variables"
  47. #endif
  48. }
  49. #define TALON_MAXENV 4096
  50. char * talon_getenv(char name[])
  51. {
  52. #if defined(HAS_SETENV)
  53. char *val = getenv(name);
  54. char *dest = NULL;
  55. if (val)
  56. {
  57. dest = malloc(strlen(val) + 1);
  58. if (dest)
  59. {
  60. strcpy(dest,val);
  61. }
  62. }
  63. return dest;
  64. #elif defined(HAS_SETENVIRONMENTVARIABLE)
  65. char *val = malloc(TALON_MAXENV);
  66. if (0 != GetEnvironmentVariableA(name,val,TALON_MAXENV-1))
  67. return val;
  68. else
  69. return NULL;
  70. #else
  71. # error "Need a function for setting environment variables"
  72. #endif
  73. }
  74. extern int sim_mallocs(int totalallocs, long allocbeforefree);
  75. typedef struct {
  76. long filedata; /* roughly 800k */
  77. int copylen; /* length of string copies */
  78. int copycount;
  79. int totalallocs;
  80. long allocsbeforefree; /* how many mallocs before free */
  81. } params;
  82. params p = {850000L,256,7966,10000,1000};
  83. char help[]="\nsimmer: a compiler simulator for performance testing\n\
  84. --filedata=<n> (default 850000L)\n\
  85. --copylen=<n> (default 256)\n\
  86. --copycount=<n> (default 7966)\n\
  87. --totalallocs=<n> (default 10000) \n\
  88. --allocsbeforefree<n> (default 1000)\n\n";
  89. int opt_process(params *p, int argc, char *argv[])
  90. /*
  91. Windows doesn't have getopt() and trying to copy open source implementations is tricky so
  92. this function will have to simulate what's "right" for the moment.
  93. */
  94. {
  95. int i;
  96. for (i=1; i < argc; i++)
  97. {
  98. if (0 == strncmp(argv[i], "--filedata=", sizeof("--filedata=")-1))
  99. {
  100. p->filedata = atol(argv[i] + sizeof("--filedata="));
  101. } else if (0 == strncmp(argv[i], "--copylen=", sizeof("--copylen=")-1)) {
  102. p->copylen = atoi(argv[i] + sizeof("--copylen="));
  103. } else if (0 == strncmp(argv[i], "--copycount=", sizeof("--copycount=")-1)) {
  104. p->copycount = atoi(argv[i] + sizeof("--copycount="));
  105. } else if (0 == strncmp(argv[i], "--totalallocs=", sizeof("--totalallocs=")-1)) {
  106. p->totalallocs = atoi(argv[i] + sizeof("--totalallocs="));
  107. } else if (0 == strncmp(argv[i], "--allocsbeforefree=", sizeof("--allocsbeforefree=")-1)) {
  108. p->allocsbeforefree = atol(argv[i] + sizeof("--allocsbeforefree="));
  109. } else if (0 == strncmp(argv[i], "-h", sizeof("-h")-1)) {
  110. fprintf(stderr,"%s", help);
  111. } else {
  112. fprintf(stderr,"%s", help);
  113. exit(0);
  114. }
  115. }
  116. }
  117. int main(int argc, char *argv[])
  118. {
  119. char *recipe = NULL;
  120. int returncode = 0;
  121. long j;
  122. opt_process(&p, argc, argv);
  123. sim_mallocs(p.totalallocs,p.allocsbeforefree);
  124. /*******************************/
  125. FILE *f;
  126. /* Real example:
  127. fwrite count: 19110
  128. fwrite totalbytes: 202876
  129. avgbytes = 11
  130. */
  131. f = tmpfile();
  132. for (j=0; j < p.filedata/11 + 1; j++)
  133. {
  134. fwrite("AAAAAAAAAA\n",1,11, f);
  135. }
  136. fflush(f);
  137. /* Something CPU-ish */
  138. long X;
  139. /* The size of of each string copy cannot be easily determined for the
  140. program that's being simulated so this is just a guess: */
  141. char *buf1 = malloc(p.copylen);
  142. char *gap = malloc(p.copylen);
  143. char *buf2 = malloc(p.copylen);
  144. /* one measured instance of the compiler did this number of string copies
  145. whilst compiling a trivial-sized program : */
  146. int c,i,d;
  147. for (i = 0; i < p.copylen - 1; i++)
  148. buf2[i] = 'A';
  149. buf2[p.copylen-1] = '\0';
  150. for (c = 0; c < p.copycount; c++)
  151. {
  152. strcpy(buf1, buf2);
  153. buf1[0]='B'; /* try to spoil the cache */
  154. for (d = 0; buf2[d] != '\0'; d++)
  155. {
  156. buf1[d]=toupper(tolower(buf1));
  157. }
  158. }
  159. free(buf2);
  160. free(gap);
  161. free(buf1);
  162. buf2 = buf1 = gap = NULL;
  163. /*******************************/
  164. /* read in the data we wrote out */
  165. rewind(f);
  166. while (!feof(f))
  167. {
  168. fgetc(f);
  169. }
  170. fclose(f);
  171. return returncode;
  172. }