source: tests/zombies/string-perf/prog-find.cfa @ 08ed947

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 08ed947 was 08ed947, checked in by Michael Brooks <mlbrooks@…>, 2 years ago

Roll up of string changes for performance testing/improvement, and a couple API features supporting them.

String API changes:
Defining a tuning knob to control the heap growth policy (relapaces former 10% hardcode, downgraded to a default)
Implementing findFrom (allowing find-non-first); leaving find as find-first.

String implementation perf improvements:
Calling C-malloc directly instead of via CFA-alloc.
Replacings loops that copy with memmove calls.
Replacings loops that search for a value with memchr calls.

String perf testing realized:
Makefile supporting several prog-*.cfa, chosen by OPERATION value (implies prog.cfa changes to support the adjusted protocol)
Adjusting the starter/accumulater declarations in PEQ and PTA to behave consistently in cfa v cpp.
Adding tests: allocation, find, normalize, pass-by-val, pass-by-x.
Adding helper shell scripts for: generating flame graphs, collecting/crunching allocation stats using Mubeen's malloc wrappers

  • Property mode set to 100644
File size: 4.5 KB
Line 
1#if defined IMPL_STL_NA_NA
2  #define IMPL_STL
3#endif
4
5#if defined IMPL_BUHR94_NA_NA
6  #define IMPL_BUHR94
7#endif
8
9#if defined IMPL_STL
10  #include <string>
11  #include <iostream>
12  #include <cstdio>
13  using namespace std;
14  #define IMPL_CXX
15
16#elif defined IMPL_CFA_HL_SHARE
17  #define IMPL_CFA_HL
18  #define IMPL_CFA
19
20#elif defined IMPL_CFA_LL_SHARE
21  #define IMPL_CFA_LL
22  #define IMPL_CFA
23
24#elif defined IMPL_CFA_HL_NOSHARE
25  #define IMPL_CFA_HL
26  #define CFA_NOSHARE
27  #define IMPL_CFA
28
29#elif defined IMPL_CFA_LL_NOSHARE
30  #define IMPL_CFA_LL
31  #define CFA_NOSHARE
32  #define IMPL_CFA
33
34#elif defined IMPL_BUHR94
35  #include <iostream>
36  #include <cstdio>
37  #include "/u0/mlbrooks/usys1/sm/string/StringSharing/src/string.h"
38  #define IMPL_CXX
39
40#else
41  #error Bad IMPL_
42#endif
43
44
45#if defined IMPL_CFA_HL
46  #include <string.hfa>
47#elif defined IMPL_CFA_LL
48  #include <string_res.hfa>
49  #include "not_string_res.hfa"
50#endif
51
52#if defined CFA_NOSHARE
53  #include <string_sharectx.hfa>
54  #define STRING_SHARING_CONTROL \
55    string_sharectx c = { NO_SHARING };
56#else
57  #define STRING_SHARING_CONTROL
58#endif
59
60#if defined IMPL_CFA
61  #include <math.hfa>
62  extern "C" {
63    void malloc_stats( void );
64  }
65#elif defined IMPL_CXX
66  #include <algorithm>
67  using std::min;
68  #include <malloc.h>
69#endif
70
71#include <time.h>
72#include <stdlib.h> // atoi
73#include <string.h> // strlen, only during setup
74#include <limits.h> // LONG_MAX
75
76#if defined IMPL_STL || defined IMPL_BUHR94
77    #define PRINT(s) std::cout << s << std::endl
78#elif defined IMPL_CFA_HL || defined IMPL_CFA_LL
79    #define PRINT(s) sout | s;
80#else
81    #error Unhandled print case
82#endif
83
84#if defined IMPL_CFA_LL
85    #define STRING_T string_res
86    #define ASSIGN_CHAR(str, idx, val) assignAt(str, idx, val)
87#else
88    #define STRING_T string
89    #define ASSIGN_CHAR(str, idx, val) str[idx] = val
90#endif
91
92double meanLen(int N, char ** strings) {
93    int totalLen = 0;
94    for (int i = 0 ; i < N; i ++) {
95        totalLen += strlen(strings[i]);
96    }
97    return (double)totalLen / (double)N;
98}
99
100volatile int checkthis = 0;
101#define MAYBE( op ) if (checkthis) { op; }
102
103
104int main( int argc, char ** argv ) {
105
106    STRING_SHARING_CONTROL
107
108
109    const char * usage_args[] = {"(Ignored)    ExecTimeSecs   Corpus...",
110                                 "(Ignored) -w WorkAllocCount Corpus..."};
111
112    const int static_arg_posns = 3;
113    int used_arg_posns = static_arg_posns;
114
115    int execTimeSecs = -1;
116
117    long int iterationCountTarget = -1;
118
119    switch (min(argc, static_arg_posns)) {
120      case 3: if ( strcmp(argv[2], "-w") == 0 ) {
121                used_arg_posns ++;
122                execTimeSecs = 0;
123                iterationCountTarget = atoi(argv[3]);
124              } else {
125                execTimeSecs = atoi(argv[2]);
126                iterationCountTarget = LONG_MAX;
127              }
128    }
129
130    int corpuslen = argc - used_arg_posns;
131    char ** corpus = argv + used_arg_posns;
132
133    if ((execTimeSecs < 1 && iterationCountTarget < 1)  || corpuslen < 1) {
134      for (int u = 0; u < sizeof(usage_args) / sizeof(*usage_args); u++) {
135        printf("usage: %s %s\n", argv[0], usage_args[u]);
136      }
137      printf("output:\nxxx,corpusItemCount,corpusMeanLenChars,callDoneActualCount,execTimeActualSec\n");
138      exit(1);
139    }
140
141    double meanCorpusLen = meanLen(corpuslen, corpus);
142
143    clock_t start, end_target, end_actual;
144
145    STRING_T corpus_imported[corpuslen];
146
147    for (int i = 0; i < corpuslen; i++) {
148        corpus_imported[i] = corpus[i];
149        // if the callee ever modifies, then we will drive GCs, which will visit the entire corpus
150    }
151
152    start = clock();
153    if (execTimeSecs != 0) {
154      end_target = start + CLOCKS_PER_SEC * execTimeSecs;
155    } else {
156      end_target = start + CLOCKS_PER_SEC * 3600;
157    }
158
159    unsigned int t = 0;
160    for ( ; t < iterationCountTarget && ((t+1) % 10000 != 0 || clock() < end_target) ; t += 1 ) {
161            #if defined OP_PFI
162              size_t foundAt =
163                #if defined IMPL_CFA
164                   find( corpus_imported[t % corpuslen], ' ')
165                #elif defined IMPL_STL
166                   corpus_imported[t % corpuslen].find(' ')
167                #else
168                  #error Bad IMPL
169                #endif
170                ;
171            #else
172              #error Bad OP_
173            #endif
174            MAYBE( PRINT( foundAt ) );
175    }
176    end_actual = clock();
177    unsigned int callsDone = t;
178    double elapsed = ((double) (end_actual - start)) / CLOCKS_PER_SEC;
179    printf("xxx,%d,%f,%d,%f\n", corpuslen, meanCorpusLen, t, elapsed);
180
181    // malloc_stats();
182
183    return 0;
184}
Note: See TracBrowser for help on using the repository browser.