-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanydl.c
1216 lines (1151 loc) · 39.1 KB
/
manydl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
* file manydl.c from https://github.com/bstarynk/misc-basile/
* SPDX-License-Identifier: MIT
*
* generating lots of C functions, and dynamically compiling and loading them
* this is completely useless, except for testing & benchmarking
*
* © Copyright Basile Starynkevitch 2004- 2024
*
* This was released up to december 18th 2022 under GPLv3+ license.
* On Dec 19, 2022 relicensed under MIT licence
*
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE manydl.c SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
************
* €Français: ce programme sous licence libre MIT pour Linux génère une
* grande quantité de greffons sous forme de code C, puis les compile
* un par un, puis les charge dynamiquement. Il peut servir à tester
* un compilateur C, et à montrer en pratique que plusieurs milliers
* de greffons sont chargeables par dlopen et dlsym. On peut le
* compiler par make manydl, puis lancer par exemple la commande
* "./manydl -n 1000 -s 500" pour générer 1000 greffons d'environ 500
* lignes en moyenne. Les fichiers C générés sont nommés comme
* _genf_N_O6.c et les greffons correspondants _genf_N_06.so
***/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <dlfcn.h>
#include <time.h>
#include <ctype.h>
#include <assert.h>
#include <math.h>
#include <sys/times.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/utsname.h>
#ifndef MANYDL_GIT
#error missing MANYDL_GIT string as preprocessing flag
#endif /*MANYDL_GIT */
// €Français: identifiant git
const char manydl_git[] = MANYDL_GIT;
#define NAME_BUFLEN 32
// €Français: nombre de fichiers C et greffons générés. Tous les
// fichiers C générés sont nommés _genf_*.c et sont générés ensemble,
// et seulement ensuite compilés en des greffons.
int maxcnt = 200; /* number of generated plugins */
#define MINIMAL_COUNT 10
// €Français: la taille détermine à peut près linéairement le nombre
// de lignes de code générés (à un facteur 5 ou 10 près).
int meansize = 400; /* mean size of generated C file */
#define MINIMAL_SIZE 15
// €Français: les greffons sont compilés en parallèle par make, par
// défaut 4 compilations en parallèle.
int makenbjobs = 5;
#define MINIMAL_NBJOBS 4
#define MAXIMAL_NBJOBS 50
// €Français: drapeau pour la verbosité à l'exécution.
bool verbose = false;
bool didcleanup = false;
// €Français: drapeau pour générer le code C sans le compiler.
// when fakerun is true, don't compile or dlopen the plugins
bool fakerun = false;
// a potential script to run on C code
const char *terminatingscript = NULL;
long random_seed;
const char *pluginsuffix = ".so";
const char *makeprog = "/usr/bin/make";
extern void show_help (void);
extern void show_version (void);
struct utsname my_uts;
/** €Français: signature de chaque fonction générée. Dans le fichier
* généré _genf_D_52.c la fonction générée est:
* int genf_D_52(int a, int b)
**/
typedef int (*funptr_t) (int, int); /* type of function pointers */
funptr_t *funarr = NULL;
char **namarr = NULL; /* array of names */
void **hdlarr = NULL; /* array of dlopen handles */
char myhostname[64];
char *progname;
#define INDENT_PROGRAM "/usr/bin/indent"
double start_elapsed_clock = NAN, start_cpu_clock = NAN;
double generate_elapsed_clock = NAN, generate_cpu_clock = NAN;
double compile_elapsed_clock = NAN, compile_cpu_clock = NAN;
double dlopen_elapsed_clock = NAN, dlopen_cpu_clock = NAN;
double compute_elapsed_clock = NAN, compute_cpu_clock = NAN;
double terminating_elapsed_clock = NAN, terminating_cpu_clock = NAN;
extern long dynstep;
extern void say_fun_a_b_c_d (const char *fun, int a, int b, int c, int d);
/* dynstep, tab & say_fun_a_b_c_d are used in generated files */
long dynstep; /* number of executed lines */
#define MAXTAB 256 /* length of tab should be a power of two */
int tab[MAXTAB]; /* a table */
extern int tab[MAXTAB];
extern int add_x_3_y (int, int);
int
add_x_3_y (int x, int y)
{
return x + 3 * y;
} /* end add_x_3_y */
extern double my_clock (clockid_t);
double
my_clock (clockid_t clid)
{
struct timespec ts = { 0, 0 };
if (clock_gettime (clid, &ts))
{
perror ("clock_gettime");
exit (EXIT_FAILURE);
};
return (double) ts.tv_sec + 1.0e-9 * ts.tv_nsec;
} /* end my_clock */
void /* printing function for generated files */
say_fun_a_b_c_d (const char *fun, int a, int b, int c, int d)
{
printf ("<%s> a=%d b=%d c=%d d=%d\n", fun, a, b, c, d);
}
double secpertick;
clock_t firstclock;
#define DICE(N) ((int)(lrand48 () % (N)))
void compute_name_for_index (char name[static NAME_BUFLEN], int ix);
void
compute_name_for_index (char name[static NAME_BUFLEN], int ix)
{
assert (ix >= 0 && ix < maxcnt);
memset (name, 0, NAME_BUFLEN);
if (maxcnt < 1600)
snprintf (name, NAME_BUFLEN, "_genf_%c_%02d",
"ABCDEFGHIJKLMNOPQ"[ix % 16], ix / 16);
else if (maxcnt < 160000)
snprintf (name, NAME_BUFLEN, "_genf_%c_%04d",
"ABCDEFGHIJKLMNOPQ"[ix % 16], ix / 16);
else if (maxcnt < 16000000)
snprintf (name, NAME_BUFLEN, "_genf_%c_%06d",
"ABCDEFGHIJKLMNOPQ"[ix % 16], ix / 16);
else
snprintf (name, NAME_BUFLEN, "_genf_%c_%08d",
"ABCDEFGHIJKLMNOPQ"[ix % 16], ix / 16);
} /* end compute_name_for_index */
/* generate a file containing one single randomly coded function
(named like the file, ie function foo in foo.c), using the lrand48
random number generator; return the size */
int
generate_file (const char *name)
{
char pathsrc[100];
FILE *f = NULL;
int l = 0;
int i = 0;
int prevjmpix = 0;
#define MAXLAB 32
bool definedlab[MAXLAB];
bool jumpedlab[MAXLAB];
memset (pathsrc, 0, sizeof (pathsrc));
for (int il = 0; il < MAXLAB; il++)
definedlab[il] = false;
for (int il = 0; il < MAXLAB; il++)
jumpedlab[il] = false;
snprintf (pathsrc, sizeof (pathsrc) - 1, "%s.c", name);
f = fopen (pathsrc, "w");
if (!f)
{
perror (pathsrc);
exit (EXIT_FAILURE);
};
/* random length of generated function */
l = meansize / 2 + DICE (meansize);
fprintf (f, "/* generated file %s length %d meansize %d*/\n", pathsrc, l,
meansize);
fprintf (f, "extern long dynstep;\n" "extern int tab[%d];\n", MAXTAB);
fprintf (f,
"extern void say_fun_a_b_c_d(const char*fun, int a, int b, int c, int d);\n");
fprintf (f, "const char gentimestamp_%s[] = __DATE__ \"@\" __TIME__;\n",
name);
fprintf (f, "int %s(int a, int b) {\n", name + 1);
fputs (" int c=0, d=1, e=2, f=3, g=4, h=5, i=6, j=7, k=8, l=a+b;\n", f);
fputs (" long initdynstep = dynstep;\n", f);
#define RANVAR ('a'+DICE(12))
for (i = 0; i < l; i++)
{
switch (DICE (16))
{
case 0:
fprintf (f, "// from %d\n", __LINE__);
fprintf (f, " %c = (%c * %d + %c * %d + %d) & 0xffffff;\n",
RANVAR, RANVAR, 2 + DICE (8), RANVAR, 3 + 2 * DICE (8),
DICE (32));
if (DICE (8) == 0)
fprintf (f, " dynstep++;\n");
break;
case 1:
fprintf (f, "// from %d\n", __LINE__);
fprintf (f,
" %c = (%c * %d + tab[%c & %#x] - %c) & 0xffffff; tab[%d]++;\n",
RANVAR, RANVAR, 1 + DICE (16), RANVAR, MAXTAB - 1, RANVAR,
DICE (MAXTAB));
if (DICE (8) == 0)
fprintf (f, " dynstep++;\n");
break;
case 2:
fprintf (f, "// from %d\n", __LINE__);
fprintf (f,
" if (%c > %c + %d) %c++; else %c=(tab[%d] + %c) & 0xffffff;\n",
RANVAR, RANVAR, 5 + DICE (10), RANVAR, RANVAR,
DICE (MAXTAB), RANVAR);
if (DICE (8) == 0)
fprintf (f, " dynstep++;\n");
if (DICE (5) == 0)
{
char v1 = RANVAR;
int nbcas = DICE (10) + 2;
fprintf (f, " switch ((%c & 0xfff) %% %d) {\n", v1, nbcas);
for (int ca = 0; ca < nbcas; ca++)
{
fprintf (f, " case %d:\n", ca);
fprintf (f, " %c %c= (%c & 0xfff) + %d;\n",
RANVAR, "+-"[DICE (2)],
RANVAR, 3 + DICE (15 + nbcas));
fprintf (f, " break;\n");
}
fprintf (f, " } //end switch from %d\n", __LINE__);
}
break;
case 3:
fprintf (f, "// from %d\n", __LINE__);
fprintf (f, " tab[%c & %#x] += %d + ((%c - %c) & 0x3ff); %c++;\n",
RANVAR, MAXTAB - 1, DICE (8) + 2, RANVAR, RANVAR, RANVAR);
if (DICE (8) == 0)
fprintf (f, " dynstep++;\n");
break;
case 4:
{
fprintf (f, "// from %d\n", __LINE__);
char lvar = RANVAR;
fprintf (f,
" while (%c>0) { dynstep++; %c -= (%c/3) + %d; }; %c=%d+%c;\n",
lvar, lvar, lvar, DICE (16) + 10, lvar, DICE (8),
RANVAR);
}
break;
case 5:
fprintf (f, "// from %d\n", __LINE__);
fprintf (f, " %c++, %c++;\n", RANVAR, RANVAR);
if (DICE (8) == 0)
fprintf (f, " %c++, %c++;\n", RANVAR, RANVAR);
if (DICE (16) == 0)
fprintf (f, " %c += (%c &0xfffff);\n", RANVAR, RANVAR);
if (DICE (16) == 0)
fprintf (f, " %c -= (1+ (%c&0x7ffff));\n", RANVAR, RANVAR);
if (DICE (8) == 0)
{
fprintf (f, " %c++, %c++;\n", RANVAR, RANVAR);
fprintf (f, " dynstep++;\n");
};
break;
case 6:
{
fprintf (f, "// from %d\n", __LINE__);
int labrank = DICE (MAXLAB);
fprintf (f, " dynstep+=%d;\n", i - prevjmpix);
prevjmpix = i;
if (!jumpedlab[labrank])
{
fprintf (f, " if (%d*%c<%d+%c) {%c++; goto lab%d;};\n",
DICE (8) + 2, RANVAR, DICE (10), RANVAR, RANVAR,
labrank);
jumpedlab[labrank] = true;
}
fprintf (f, "%c--;\n", RANVAR);
if (!definedlab[labrank])
{
fprintf (f, "lab%d: %c++;\n", labrank, RANVAR);
definedlab[labrank] = true;
}
}
break;
case 7:
{
fprintf (f, "// from %d\n", __LINE__);
fprintf (f,
" %c = (%c / ((%c & 0xffff) + 2) + %c * %d) & 0xffffff;\n",
RANVAR, RANVAR, RANVAR, RANVAR, DICE (10) + 3);
char v1 = RANVAR;
char v2 = RANVAR;
while (v1 == v2)
v2 = RANVAR;
fprintf (f, " if (%c > %c)\n", v1, v2);
fprintf (f, " tab[(%c & 0xfffff) %% %#x]++;\n",
RANVAR, MAXTAB - DICE (MAXTAB / 10));
if (DICE (100) > 20)
fprintf (f, " %c += %d;\n", RANVAR, DICE (12) + 5);
}
break;
case 8:
fprintf (f, "// from %d\n", __LINE__);
fprintf (f,
" %c = (%d + ((%c << (1+ (%c & 0x1f))) + %c)) & 0xffffff;\n",
RANVAR, DICE (32) + 5, RANVAR, RANVAR, RANVAR);
if (DICE (100) > 10)
fprintf (f,
" %c = (%c %% %d) + tab[(%c & 0xfffff) %% %d];\n",
RANVAR, RANVAR, (DICE (100) + 10),
RANVAR, 2 + DICE (3 * MAXTAB / 4));
break;
case 9:
{
char v = RANVAR;
int labrank = DICE (MAXLAB);
fprintf (f, "// from %d\n", __LINE__);
fprintf (f, " %c = (%c * %d + %d) & 0xffffff;\n",
v, RANVAR, DICE (100) + 7, DICE (200) + 12);
fprintf (f,
" if (dynstep++ %% %d == (%c & 0x1ff) && dynstep < initdynstep + %d)\n",
(DICE (50) + 2), v, 100 + DICE (1000));
fprintf (f, " goto lab%d;\n", labrank);
jumpedlab[labrank] = true;
}
break;
case 10:
fprintf (f, "// from %d\n", __LINE__);
fprintf (f, " tab[%d]++, tab[%c & %#x] += (%c & 0xff) + %d;\n",
DICE (MAXTAB), RANVAR, MAXTAB - 1, RANVAR, DICE (8) + 2);
fprintf (f,
" if (tab[%d] %% %d == 0 && dynstep > initdynstep + %d)\n"
" goto end_%s;\n", DICE (MAXTAB), (40 + DICE (50)),
meansize + 50 + 20 * DICE (1000), name);
break;
case 11:
fprintf (f, "// from %d\n", __LINE__);
fprintf (f, " %c = %c + %d;\n", RANVAR, RANVAR, (2 + DICE (5)));
fprintf (f, " %c = (%c*%d) + (%c>>%d);\n",
RANVAR, RANVAR, (5 + DICE (32)), RANVAR, (1 + DICE (4)));
fprintf (f,
" if (%c > %d)\n"
" %c = %c + (tab[%d] %% %d);\n",
RANVAR, DICE (1000) + 100, RANVAR, RANVAR,
DICE (MAXTAB / 2), (2 + DICE (1024)));
break;
case 12:
fprintf (f, "// from %d\n", __LINE__);
fprintf (f, " %c = %d;\n", RANVAR, 5 + DICE (20));
fprintf (f, //
" if (tab[%d] > %c)\n"
" %c = %c + (tab[%d] & 0xffff);\n",
DICE (MAXTAB / 2), RANVAR,
RANVAR, RANVAR, DICE (MAXTAB / 2));
break;
case 13:
{
fprintf (f, "// from %d\n", __LINE__);
char fvar = RANVAR;
fprintf (f,
" for (%c &= %d; %c>0; %c--) {dynstep++;tab[(%c & 0xffff) %% %d] += (1+(%c&0x1f));};\n",
fvar, MAXTAB - 1, fvar, fvar, fvar, MAXTAB, RANVAR);
if (DICE (5) == 0)
fprintf (f, " %c += %d;\n", RANVAR, 5 + DICE (20));
}
break;
case 14:
{
fprintf (f, "// from %d\n", __LINE__);
char lvar = RANVAR, rvar = RANVAR;
if (lvar != rvar)
fprintf (f, " %c = %c;\n", lvar, rvar);
else
fprintf (f, " %c++;\n", lvar);
fprintf (f,
" tab[(%c & 0x3ffff) %% %d] %c= ((%c & 0xffff) + %d);\n",
RANVAR, (MAXTAB / 2 + DICE (MAXTAB / 3)) % MAXTAB,
"+-*/%"[DICE (5)], RANVAR, DICE (64) + 4);
fprintf (f, " if (tab[%d] > %c)\n" //
" %c = (%c * %d) + tab[%d];\n",
(2 + DICE (MAXTAB / 3)) % MAXTAB, RANVAR,
RANVAR, RANVAR, 5 + DICE (50), DICE (2 * MAXTAB / 3));
};
break;
case 15:
{
fprintf (f, "// from %d\n", __LINE__);
int labrank = DICE (MAXLAB);
fprintf (f, " %c = %c + %d;\n", RANVAR, RANVAR, 2 + DICE (5));
if (DICE (3) == 0)
fprintf (f, " %c = (%c + %d) & 0xfffff;\n", RANVAR, RANVAR,
9 + DICE (35));
if (DICE (8) == 0)
fprintf (f, " tab[(%c & 0x1fff) %% %d] += %c;\n", RANVAR,
DICE (MAXTAB / 3) + 1, RANVAR);
fprintf (f, " dynstep++;\n");
fprintf (f, " if (dynstep < initdynstep + %d)\n",
DICE (16384) + 10);
fprintf (f, " goto lab%d;\n", labrank);
jumpedlab[labrank] = true;
break;
};
};
fprintf (f, " dynstep+=%d;\n", i - prevjmpix);
};
for (i = 0; i < MAXLAB; i++)
{
if (jumpedlab[i] && !definedlab[i])
fprintf (f, " lab%d:\n %c++;\n", i, RANVAR);
if (DICE (8) == 0)
{
char v = RANVAR;
fprintf (f, " %c = (%c + %d) & 0xfffff;\n", v, v, 10 + DICE (25));
}
if (DICE (4) == 0 || i == MAXLAB / 2)
{
fprintf (f, " tab[(%c &0x3fffff) %% %d] += %c;\n", RANVAR, MAXTAB,
RANVAR);
fprintf (f, " %c += (%c&0xffff);\n", RANVAR, RANVAR);
fprintf (f, " dynstep++;\n");
};
}
fprintf (f, " a &= 0xffffff;\n");
fprintf (f, "end_%s:\n", name);
fprintf (f, " say_fun_a_b_c_d(\"%s\", a, b, c, d);\n", name);
fprintf (f, " return a;\n" "} /* end %s of %d instr */\n", name + 1, l);
fprintf (f, "\n\n\n"
"\n/* file %s was generated by " __FILE__ "*/\n", pathsrc);
fprintf (f, "\n" //
"/****** for Emacs...\n" //
" ** Local Variables: ;;\n" //
);
fprintf (f, //
" ** compile-command: \"make %s%s\" ;;\n" //
" ** End: ;;\n" //
" *******/\n", name, pluginsuffix);
fflush (f);
fclose (f);
/* run GNU indent on the generated C code */
if (!access (INDENT_PROGRAM, X_OK))
{
// €Français: le code C généré est indenté par GNU indent.
char indcmd[128];
memset (indcmd, 0, sizeof (indcmd));
snprintf (indcmd, sizeof (indcmd), "%s %s", INDENT_PROGRAM, pathsrc);
int err = system (indcmd);
if (err)
{
char cwdbuf[256];
memset (cwdbuf, 0, sizeof (cwdbuf));
if (!getcwd (cwdbuf, sizeof (cwdbuf) - 2))
strcpy (cwdbuf, "./");
fprintf (stderr, "%s: failed to run %s (%d) in %s\n", progname,
indcmd, err, cwdbuf);
exit (EXIT_FAILURE);
};
}
return l;
} /* end generate_file */
/* return a *static* string containing the self & child CPU times */
const char *
timestring ()
{
static char timbuf[120];
struct tms ti;
clock_t clock;
memset (&ti, 0, sizeof (ti));
clock = times (&ti);
memset (timbuf, 0, sizeof (timbuf));
snprintf (timbuf, sizeof (timbuf) - 1,
"CPU= %.3fu+%.3fs self, %.3fu+%.3fs children; real= %.3f",
secpertick * ti.tms_utime, secpertick * ti.tms_stime,
secpertick * ti.tms_cutime, secpertick * ti.tms_cstime,
secpertick * (clock - firstclock));
return timbuf;
} // end timestring
void
show_help (void)
{
printf ("%s usage (MIT licensed, no warranty)\n", progname);
printf ("\t a nearly useless program generating many dlopen-ed plugins\n");
printf ("\t --version | -V : shows version information\n");
printf ("\t --help | -h : shows this help\n");
printf ("\t -v : run verbosely\n");
printf ("\t -F : fake run - dont compile or run plugins,\n"
"\t ... just generate C code.\n");
printf ("\t -n <count> : set number of generated plugins,"
" default is %d\n", maxcnt);
printf ("\t -s <mean-size> : set mean size of generated plugins,"
" default is %d\n", meansize);
printf ("\t -j <job> : number of jobs, passed to make,"
" default is %d\n", makenbjobs);
printf ("\t -m <maker> : make program, default is %s\n", makeprog);
printf
("\t -R <randomseed> : seed passed to srand48, default is unique\n");
printf ("\t -S <p.suffix> : plugin suffix, default is %s\n",
pluginsuffix);
printf ("\t -T <script> : terminating popen-ed script\n");
printf ("\t (could be some external analyzer,\n"
"\t ... getting names of generated C files)\n");
printf ("\t -C : clean up the mess (old _genf_* files)\n");
} /* end of show_help */
void
show_version (void)
{
printf ("%s git %s on %s at built %s\n", progname, manydl_git, myhostname,
__DATE__);
exit (EXIT_SUCCESS);
} /* end of show_version */
void
cleanup_the_mess (void)
{
// €Français: suppression de tous les fichiers générés
char **oldnamarr = NULL;
char cwdbuf[256];
int nbclean = 0;
int oldnamsiz = ((2 * maxcnt + 40) | 0x3f) + 1;
struct dirent *curent = NULL;
double startelapsedclock = my_clock (CLOCK_MONOTONIC);
double startcpuclock = my_clock (CLOCK_PROCESS_CPUTIME_ID);
oldnamarr = calloc (oldnamsiz, sizeof (char *));
if (!oldnamarr)
{
fprintf (stderr,
"%s: failed to calloc oldnamarr for %d string pointers (%s)\n",
progname, oldnamsiz, strerror (errno));
exit (EXIT_FAILURE);
};
memset (cwdbuf, 0, sizeof (cwdbuf));
if (!getcwd (cwdbuf, sizeof (cwdbuf) - 2))
{
fprintf (stderr, "%s: failed to getcwd (%s)\n", progname,
strerror (errno));
};
DIR *curdir = opendir (cwdbuf);
if (!curdir)
{
fprintf (stderr,
"%s: failed to opendir the current directory %s for %d string pointers (%s)\n",
progname, cwdbuf, oldnamsiz, strerror (errno));
exit (EXIT_FAILURE);
};
int oldnbnames = 0;
while ((curent = readdir (curdir)) != NULL)
{
char c = 0;
int ix = -1;
int pos = -1;
if (curent->d_type != DT_REG /*not regular file */ )
continue;
if (curent->d_name[0] == 'g'
&& !strncmp (curent->d_name, "genf_", 5)
&& sscanf (curent->d_name, "genf_%c_%d%n", &c, &ix, &pos) >= 2
&& ix >= 0
&& pos > 0 && curent->d_name[pos] == '.' && c >= 'A' && c <= 'Z')
{
if (oldnbnames + 1 >= oldnamsiz)
{ // should grow oldnamarr
int biggernamsiz =
1 + ((oldnamsiz + oldnamsiz / 4 + 10) | 0x1f);
char **biggernamarr = calloc (biggernamsiz, sizeof (char *));
if (!biggernamarr)
{
fprintf (stderr,
"%s: failed to calloc biggernamarr for %d string pointers (%s)\n",
progname, biggernamsiz, strerror (errno));
exit (EXIT_FAILURE);
};
memcpy (biggernamarr, oldnamarr, oldnamsiz * sizeof (char *));
oldnamsiz = biggernamsiz;
free (oldnamarr);
oldnamarr = biggernamarr;
}; // end growing up the oldnamarr
char *dupcurnam = strdup (curent->d_name);
if (!dupcurnam)
{
fprintf (stderr,
"%s: failed in the current directory %s for %d string pointers to strdup name '%s' (%s)\n",
progname, cwdbuf, oldnamsiz, curent->d_name,
strerror (errno));
break;
}
oldnamarr[oldnbnames++] = dupcurnam;
}
};
closedir (curdir), curdir = NULL;
for (int k = 0; k < oldnbnames; k++)
{
if (unlink (oldnamarr[k]))
{
fprintf (stderr,
"%s: failed in current directory %s to unlink '%s' (%s)\n",
progname, cwdbuf, oldnamarr[k], strerror (errno));
}
else
nbclean++;
free (oldnamarr[k]), oldnamarr[k] = NULL;
};
free (oldnamarr), oldnamarr = NULL;
double endelapsedclock = my_clock (CLOCK_MONOTONIC);
double endcpuclock = my_clock (CLOCK_PROCESS_CPUTIME_ID);
printf
("%s: cleaned and removed %d files starting with genf_ in %s in %.4f elapsed, %.4f cpu sec\n",
progname, nbclean, cwdbuf, endelapsedclock - startelapsedclock,
endcpuclock - startcpuclock);
didcleanup = true;
} /* end cleanup_the_mess */
// €Français: traitement des options du programme
void
get_options (int argc, char **argv)
{
bool seeded = false;
int opt = 0;
while ((opt = getopt (argc, argv, "hVCFvn:s:j:m:S:R:T:")) > 0)
{
switch (opt)
{
case 'h': /* help */
show_help ();
return;
case 'V': /* version */
show_version ();
return;
case 'v': /* verbose */
verbose = true;
break;
case 'F': /* fake run */
fakerun = true;
break;
case 'T':
terminatingscript = optarg;
break;
case 'C': /* cleanup - remove all previous plugins
and previously generated genf*.c files */
cleanup_the_mess ();
break;
case 'n': /* number of plugins */
maxcnt = atoi (optarg);
if (maxcnt < MINIMAL_COUNT)
{
fprintf (stderr,
"%s: plugin count given by -n should be at least %d\n",
progname, MINIMAL_COUNT);
exit (EXIT_FAILURE);
};
break;
case 's': /* mean size */
meansize = atoi (optarg);
if (meansize < MINIMAL_SIZE)
{
fprintf (stderr,
"%s: mean size given by -s should be at least %d\n",
progname, MINIMAL_SIZE);
exit (EXIT_FAILURE);
}
break;
case 'm': /* make program */
if (strlen (optarg) < 3 || optarg[0] == '.')
{
fprintf (stderr,
"%s: make program given by -m %s should be a command\n"
"... and not starting with a dot\n", progname, optarg);
exit (EXIT_FAILURE);
}
makeprog = optarg;
break;
case 'S': /* plugin suffix */
if (optarg[0] != '.' && !isalpha (optarg[1]))
{
fprintf (stderr,
"%s: plugin suffix given by -S %s should start with a dot\n"
"... then a letter\n", progname, optarg);
exit (EXIT_FAILURE);
}
pluginsuffix = optarg;
break;
case 'R':
random_seed = atol (optarg);
seeded = true;
break;
case 'j':
makenbjobs = atoi (optarg);
if (makenbjobs < MINIMAL_NBJOBS || makenbjobs > MAXIMAL_NBJOBS)
{
fprintf (stderr, "%s: unexpected -j %d number of make jobs\n"
"... (should be between %d and %d)\n",
progname, makenbjobs, MINIMAL_NBJOBS, MAXIMAL_NBJOBS);
};
break;
default:
fprintf (stderr, "%s: unexpected option %c\n", progname,
(char) opt);
show_help ();
exit (EXIT_FAILURE);
}
};
if (seeded)
srand48 (random_seed);
else
{
time_t t = 0;
if (!time (&t))
perror ("time");
long l = ((long) getpid ()) ^ ((long) t);
srand48 (l);
}
} /* end get_options */
// €Français: génération de tous les fichiers C _genf*.c
void
generate_all_c_files (void)
{
double startelapsedclock = my_clock (CLOCK_MONOTONIC);
double startcpuclock = my_clock (CLOCK_PROCESS_CPUTIME_ID);
printf
("%s (git %s, pid %d on %s) start generating %d C files of mean size %d\n",
progname, MANYDL_GIT, (int) getpid (), myhostname, maxcnt, meansize);
fflush (NULL);
int p = 1 + (((int) sqrt (maxcnt + maxcnt / 8)) | 0x1f);
for (int ix = 0; ix < maxcnt; ix++)
{
char curname[64];
memset (curname, 0, sizeof (curname));
compute_name_for_index (curname, ix);
generate_file (curname);
if (verbose && ix % p == 0 && ix > 10)
{
double curelapsedclock = my_clock (CLOCK_MONOTONIC);
double curcpuclock = my_clock (CLOCK_PROCESS_CPUTIME_ID);
printf
("%s: %d generated C files out of %d (so %.2f %%) in %.3f elapsed, %.3f cpu sec\n",
progname, ix, maxcnt, (100.0 * ix) / maxcnt,
curelapsedclock - startelapsedclock,
curcpuclock - startcpuclock);
fflush (NULL);
sync ();
};
}
generate_elapsed_clock = my_clock (CLOCK_MONOTONIC);
generate_cpu_clock = my_clock (CLOCK_PROCESS_CPUTIME_ID);
printf ("%s generated %d C files in %.3f elapsed sec (%.4f / file)\n",
progname, maxcnt, generate_elapsed_clock - start_elapsed_clock,
(generate_elapsed_clock - start_elapsed_clock) / (double) maxcnt);
printf ("%s generated %d C files in %.3f CPU sec (%.4f / file)\n",
progname, maxcnt, generate_cpu_clock - start_cpu_clock,
(generate_cpu_clock - start_cpu_clock) / (double) maxcnt);
fflush (NULL);
} /* end generate_all_c_files */
// €Français: compilation parallèle en des greffons de tous les
// fichiers C générés _genf*.c
void
compile_all_plugins (void)
{
char buildcmd[128];
struct rusage uscompil = { };
double childcpuclock = 0.0;
snprintf (buildcmd, sizeof (buildcmd),
"%s -j%d manydl-plugins", makeprog, makenbjobs);
printf ("%s start compiling %d plugins\n", progname, maxcnt);
fflush (NULL);
printf ("%s will do for %d plugins: %s\n", progname, maxcnt, buildcmd);
fflush (NULL);
int buildcode = system (buildcmd);
if (buildcode > 0)
{
fprintf (stderr, "%s failed to run: %s (%d)\n",
progname, buildcmd, buildcode);
};
if (!getrusage (RUSAGE_CHILDREN, &uscompil))
{
childcpuclock =
((double) uscompil.ru_utime.tv_sec +
1.0e-6 * uscompil.ru_utime.tv_usec) +
((double) uscompil.ru_stime.tv_sec +
1.0e-6 * uscompil.ru_stime.tv_usec);
}
compile_elapsed_clock = my_clock (CLOCK_MONOTONIC);
compile_cpu_clock = my_clock (CLOCK_PROCESS_CPUTIME_ID) + childcpuclock;
printf ("%s compiled %d C files in %.3f elapsed sec (%.4f / file)\n",
progname, maxcnt, compile_elapsed_clock - generate_elapsed_clock,
(compile_elapsed_clock - generate_elapsed_clock) / (double) maxcnt);
printf ("%s compiled %d C files in %.3f CPU sec (%.4f / file)\n",
progname, maxcnt, compile_cpu_clock - generate_cpu_clock,
(compile_cpu_clock - generate_cpu_clock) / (double) maxcnt);
} /* end compile_all_plugins */
// €Français: chargement des tous les greffons _genf_*.so; pour le
// fichier généré _genf_D_52.c on charge dynamiquement _genf_D_52.so
// contenant la fonction int genf_D_52(int a, int b)
void
dlopen_all_plugins (void)
{
double startelapsedclock = my_clock (CLOCK_MONOTONIC);
double startcpuclock = my_clock (CLOCK_PROCESS_CPUTIME_ID);
hdlarr = calloc ((size_t) maxcnt, sizeof (void *));
if (!hdlarr)
{
fprintf (stderr, "%s: failed to calloc hdlarr for %d plugins (%s)\n",
progname, maxcnt, strerror (errno));
exit (EXIT_FAILURE);
};
namarr = calloc ((size_t) maxcnt, sizeof (char *));
if (!namarr)
{
fprintf (stderr, "%s: failed to calloc namarr for %d plugins (%s)\n",
progname, maxcnt, strerror (errno));
exit (EXIT_FAILURE);
}
funarr = calloc ((size_t) maxcnt, sizeof (funptr_t));
if (!funarr)
{
fprintf (stderr,
"%s: failed to calloc funarr for %d function pointers (%s)\n",
progname, maxcnt, strerror (errno));
exit (EXIT_FAILURE);
}
printf ("%s start dlopen-ing %d plugins\n", progname, maxcnt);
fflush (NULL);
fflush (NULL);
for (int ix = 0; ix < maxcnt; ix++)
{
char curname[64];
memset (curname, 0, sizeof (curname));
char pluginpath[96];
memset (pluginpath, 0, sizeof (pluginpath));
compute_name_for_index (curname, ix);
namarr[ix] = strdup (curname + 1);
snprintf (pluginpath, sizeof (pluginpath), "./%s%s",
curname, pluginsuffix);
if (access (pluginpath, F_OK))
{
fprintf (stderr, "%s: cannot access plugin#%d %s (%s)\n",
progname, ix, pluginpath, strerror (errno));
exit (EXIT_FAILURE);
};
hdlarr[ix] = dlopen (pluginpath, RTLD_NOW);
if (!hdlarr[ix])
{
fprintf (stderr, "%s: cannot dlopen plugin#%d %s (%s)\n",
progname, ix, pluginpath, dlerror ());
exit (EXIT_FAILURE);
};
funarr[ix] = dlsym (hdlarr[ix], namarr[ix]);
if (!funarr[ix])
{
fprintf (stderr, "%s: cannot dlsym name %s in plugin#%d %s (%s)\n",
progname, namarr[ix], ix, pluginpath, dlerror ());
exit (EXIT_FAILURE);
};
};
double endelapsedclock = my_clock (CLOCK_MONOTONIC);
double endcpuclock = my_clock (CLOCK_PROCESS_CPUTIME_ID);
printf ("%s dlopen-ed %d plugins in %.3f elapsed sec (%.4f / plugin)\n",
progname, maxcnt, endelapsedclock - startelapsedclock,
(endelapsedclock - startelapsedclock) / (double) maxcnt);
printf ("%s dlopen-ed %d plugins in %.3f CPU sec (%.4f / plugin)\n",
progname, maxcnt, endcpuclock - startcpuclock,
(endcpuclock - startcpuclock) / (double) maxcnt);
dlopen_elapsed_clock = endelapsedclock;
dlopen_cpu_clock = endcpuclock;
fflush (NULL);
} /* end dlopen_all_plugins */
// €Français: appel aléatoire aux fonctions générées
long
do_the_random_calls_to_dlsymed_functions (void)
{
double startelapsedclock = my_clock (CLOCK_MONOTONIC);
double startcpuclock = my_clock (CLOCK_PROCESS_CPUTIME_ID);
long nbcalls = ((long) maxcnt * meansize) / 16 + 1234 + DICE (128);
printf ("%s: doing %ld random calls (git %s)\n",
progname, nbcalls, MANYDL_GIT);
fflush (NULL);
int s = maxcnt;
int r = DICE (100);
int p = 20 + DICE (10) + (int) sqrt (nbcalls);
for (long n = 0; n < nbcalls; n++)
{
int k = DICE (maxcnt);
r = (*funarr[k]) ((int) (n / 10) + DICE (maxcnt), s + r);
if (n % p == 0)
printf ("%s: after %ld calls r is %d\n", progname, n, r);
}
double endelapsedclock = my_clock (CLOCK_MONOTONIC);
double endcpuclock = my_clock (CLOCK_PROCESS_CPUTIME_ID);
printf
("%s: done %ld random calls (git %s) in %.3f elapsed %.4f cpu sec with %d plugins\n",
progname, nbcalls, MANYDL_GIT, (endelapsedclock - startelapsedclock),
(endcpuclock - startcpuclock), maxcnt);
printf ("%s: so %.3f µs elapsed %.3f µs cpu µs per call\n", progname,
1.0e+6 * (endelapsedclock - startelapsedclock) / (double) nbcalls,
1.0e+6 * (endcpuclock - startcpuclock) / (double) nbcalls);
compute_elapsed_clock = endelapsedclock;
compute_cpu_clock = endcpuclock;
fflush (NULL);
return nbcalls;
} /* end do_the_random_calls_to_dlsymed_functions */
// €Français: exécution d'un script final qui reçoit le nom des
// fichiers C et connait dans des variables d'environnement les
// paramètres décrivant ceux-ci.
void
run_terminating_script (void)
{
double startelapsedclock = my_clock (CLOCK_MONOTONIC);
double startcpuclock = my_clock (CLOCK_PROCESS_CPUTIME_ID);
char cwdbuf[128];
memset (cwdbuf, 0, sizeof (cwdbuf));
if (NULL == getcwd (cwdbuf, sizeof (cwdbuf)))
{
fprintf (stderr,
"%s: failed to getcwd when running terminating script %s (%s)\n",
progname, terminatingscript, strerror (errno));
exit (EXIT_FAILURE);
};
char termcmd[256];
memset (termcmd, 0, sizeof (termcmd));
snprintf (termcmd, sizeof (termcmd), "%s %d '%s'", terminatingscript,
maxcnt, cwdbuf);
// should putenv -in static buffers- MANYDL_GIT and MANYDL_SIZE and MANYDL_MAXCOUNT etc...
static char gitenv[64];
static char sizenv[64];
static char pidenv[64];
static char cntenv[64];
static char cwdenv[256];
// €Français: version du programme générateur
snprintf (gitenv, sizeof (gitenv), "MANYDL_GIT=%s", MANYDL_GIT);
putenv (gitenv);
// €Français: taille moyenne des fichiers C
snprintf (sizenv, sizeof (sizenv), "MANYDL_MEANSIZE=%d", meansize);