-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathunit1.pas
1385 lines (1298 loc) · 51.7 KB
/
unit1.pas
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
(******************************************************************************)
(* HeapSim 04.09.2015 *)
(* *)
(* Version : 0.03 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : This Programms implements a test framework to evaluate *)
(* different heap implementation strategies, as well as to *)
(* evaluate different allokation strategies in the daily *)
(* implementation of your programs. *)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: none *)
(* *)
(* History : 0.01 - Initial version *)
(* 0.02 - Verbesserungen in TBitVectorHeap *)
(* 0.03 - Fix Div by 0 errors on very short simulations *)
(* ADD: commalist min/maxlivetime for commalist *)
(* *)
(******************************************************************************)
(*
*
* http://www.lowlevel.eu/wiki/Heapverwaltung
* https://de.wikipedia.org/wiki/Garbage_Collection
*
*)
Unit Unit1;
{$MODE objfpc}{$H+}
Interface
Uses
Classes, SysUtils, FileUtil, TAGraph, TASeries, EpikTimer, Forms, Controls,
Graphics, Dialogs, StdCtrls, ComCtrls, ExtCtrls, ColorBox, Menus, ubaseHeap,
TACustomSeries, TATransformations;
Const
_1MB = 1024 * 1024; // Kleinste Größe für ein
(*
* Der Bitvektor Heap
* Nachteile
* - ca. 8 % des Speichers gehen durch das Bitfeld verloren.
* - Lineare Suche nach Freien Blöcken
* Vorteile
* - Keine Freispeicherliste -> Kein ResortFreeList notwendig
*)
BitVecH = 'BitVectorHeap'; // Fertig - Implementiert
(*
* Der Buddy Heap versucht die Nachteile von Bitvektorheap zu minimieren
* in dem die Suche durch einen Binärbaum ersetzt wird. Dies Kostet aber noch
* mehr speicher und erhöht die Kleinstmögliche Allokierbare Blockgröße
* drastisch.
*)
BuddyH = 'Buddy Heap';
(*
* Alle Nachfolgenden Heaps sind Freispeicherlisten Heaps.
* Sie unterscheiden sich lediglich in der Methode, nach derer neue Speicher
* blöcke Allokiert werden.
*)
FirstFitH = 'First fit Heap'; // Fertig - Implementiert
NextFitH = 'Next fit Heap'; // Fertig - Implementiert
BestFitH = 'Best fit Heap'; // Fertig - Implementiert
WorstFitH = 'Worst fit Heap'; // Fertig - Implementiert
(*
* Wrapper für die FPC Heap Implementierung
*)
FPCH = 'FPC Heap';
(* aus dem Compilerbau Skript :
Lösung 1:
Versuche Fragmentierung durch Vergabestrategie zu minimieren
Lösung 2:
Freie Speicherblöcke zusammenschieben („Kompaktifizierung“)
• zu Lösung 1: Suche eines passenden Blocks in der Freispeicherliste nach
• best-fit Methode: Auswahl des Blocks mit dem kleinsten Überschuss
⇒ kleine, unverwendbare Restblöcke bleiben über
⇒ Suche nach best-fit ist kostspielig
• first-fit Methode: Auswahl des ersten Blocks mit ausreichender Größe
⇒ kleine Teile akkumulieren am Beginn der Liste (degenerierende
Suchzeiten)
• next-fit Methode: Freispeicherliste ist Ringliste; der „Anfang“ rotiert in der Liste; sonst wie first-fit
⇒ verteilt die kleinen Reste über die Halde
• buddy Methode: suche nach genauer Größe; wenn nicht vorhanden, zerteile einen Block mit (z.B.) doppelter Größe
⇒ größere Hoffnung auf spätere Vergabe des Rests
• worst-fit Methode: Auswahl des Blocks mit größtem Überschuss
Bei allen diesen Varianten:
• Es gibt Beispiele für die Überlegenheit der jeweiligen Strategie gegenüber
allen anderen!
• Es gilt das Knuth‘sche Gesetz, dass auf Dauer diese Strategien zu einem
Zustand führen, in dem belegte zu freien Blöcken im Verhältnis 2:1 stehen.
D.h. Fragmentierung ist so nicht vermeidbar!
empirisch: next-fit oder first-fit noch „am besten“
• zu Lösung 2:
• teilweise Kompaktifizierung: Vereinigung aneinandergrenzender freier Speicherblöcke
⇒ teuer mit Freispeicherliste (Sortieren notwendig)
einfach mit BitvektorImplementierung
• vollständige Komapktifizierung:
Zusammenschieben aller belegten Blöcke
Schwierigkeit: Anpassung von Zeigern auf verschobene Bereiche (siehe
Garbage Collection wegen Erkennung der Zeiger) !! Würde nur mit Trampolin pointern gehen !!
*)
Type
{ TForm1 }
TForm1 = Class(TForm)
Button1: TButton;
Button10: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Chart1: TChart;
Chart1LineSeries1: TLineSeries;
Chart2: TChart;
Chart3: TChart;
Chart4: TChart;
Chart4BarSeries1: TBarSeries;
ChartAxisTransformations1: TChartAxisTransformations;
ChartAxisTransformations1LogarithmAxisTransform1: TLogarithmAxisTransform;
CheckBox1: TCheckBox;
CheckBox10: TCheckBox;
CheckBox2: TCheckBox;
CheckBox3: TCheckBox;
CheckBox4: TCheckBox;
CheckBox5: TCheckBox;
CheckBox6: TCheckBox;
CheckBox7: TCheckBox;
CheckBox8: TCheckBox;
CheckBox9: TCheckBox;
ColorBox1: TColorBox;
ColorBox2: TColorBox;
ColorBox3: TColorBox;
ColorBox4: TColorBox;
ColorBox5: TColorBox;
ColorBox6: TColorBox;
ColorBox7: TColorBox;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Edit1: TEdit;
Edit10: TEdit;
Edit11: TEdit;
Edit12: TEdit;
Edit13: TEdit;
Edit14: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Edit9: TEdit;
EpikTimer1: TEpikTimer;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
GroupBox4: TGroupBox;
GroupBox5: TGroupBox;
GroupBox6: TGroupBox;
GroupBox7: TGroupBox;
Label1: TLabel;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
Label13: TLabel;
Label14: TLabel;
Label15: TLabel;
Label16: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
OpenDialog1: TOpenDialog;
PageControl1: TPageControl;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
SaveDialog1: TSaveDialog;
SaveDialog2: TSaveDialog;
SaveDialog3: TSaveDialog;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
TabSheet4: TTabSheet;
Procedure Button1Click(Sender: TObject);
Procedure Button3Click(Sender: TObject);
Procedure Button4Click(Sender: TObject);
Procedure Button5Click(Sender: TObject);
Procedure Button6Click(Sender: TObject);
Procedure Button7Click(Sender: TObject);
Procedure Button8Click(Sender: TObject);
Procedure Button9Click(Sender: TObject);
Procedure Chart1DblClick(Sender: TObject);
Procedure CheckBox1Change(Sender: TObject);
Procedure CheckBox2Change(Sender: TObject);
Procedure ComboBox1KeyPress(Sender: TObject; Var Key: char);
Procedure FormCloseQuery(Sender: TObject; Var CanClose: boolean);
Procedure FormCreate(Sender: TObject);
Procedure MenuItem1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
Procedure ExportChartAsCSV(Const Chart: TChart);
Procedure ExportChartAsBitmap(Const Chart: TChart);
End;
Var
Form1: TForm1;
Heap: TBaseHeap = Nil; // Der Heap mit dem Gearbeitet werden soll
HeapSize: int64; // Die vom User gewählte HeapGröße, welche zur Verfügung steht.
Panik: Boolean; // Wenn True, dann werden alle Operationen schnellstmöglich abgebrochen und die Simulation beendet.
Version: String = ''; // Versionsstring wird in Form Create gesetzt.
Implementation
Uses ubitvectorheap, ufirstfitheap, ubestfitheap, unextfitheap, ubuddyheap,
uworstfitheap, ufpcheap, ureport, LCLIntf, math, LazUTF8, IniFiles;
{$R *.lfm}
// Liefert eine Zufallszahl in [Min_ .. Max_]
Function RandomRange(Min_, Max_: integer): Integer;
Var
d: integer;
Begin
d := max_ - min_;
result := random(d + 1) + min_;
End;
{ TForm1 }
Procedure TForm1.Button1Click(Sender: TObject);
Begin
HeapSize := StrToInt64Def(edit1.text, -1) * _1MB;
// HeapSize := 128; // debug
If (HeapSize <= 0) Or (HeapSize > 2 Shl 31) Then Begin
ShowMessage('Error invalid heap size : ' + Edit1.Text);
exit;
End;
Case ComboBox1.Text Of
BitVecH: Begin
Heap := TBitVectorHeap.Create(HeapSize);
End;
BestFitH: Begin
heap := TBestFitHeap.Create(HeapSize);
End;
WorstFitH: Begin
heap := TWorstFitHeap.Create(HeapSize);
End;
FirstFitH: Begin
heap := TFirstFitHeap.Create(HeapSize);
End;
NextFitH: Begin
heap := TNextFitHeap.Create(HeapSize);
End;
BuddyH: Begin
// Alle Zweierpotenzen sind Binär im Format "100..00" zieht man
// eins ab kommt "011..11" raus. Verundet muss das 0 Ergeben,
// wenn nicht wars keine Zweierpotenz ;)
If ((HeapSize And (HeapSize - 1)) = 0) Then Begin
heap := TBuddyHeap.create(HeapSize);
End
Else Begin
ShowMessage('Error Heapsize need to be power of 2');
End;
End;
FPCH: Begin
heap := TFPCHeap.Create(HeapSize);
End;
End;
If assigned(heap) Then Begin
Button1.Enabled := false;
Button5.Enabled := false;
Button6.Enabled := true;
Edit1.Enabled := false;
ComboBox1.Enabled := false;
End
Else Begin
showmessage('Error could not create : ' + ComboBox1.Text);
End;
End;
Procedure TForm1.Button3Click(Sender: TObject);
Begin
// Free Heap
If assigned(Heap) Then Heap.free;
heap := Nil;
Button1.Enabled := true;
Edit1.Enabled := true;
ComboBox1.Enabled := true;
Button6.Enabled := false;
Button5.Enabled := True;
End;
Procedure TForm1.Button4Click(Sender: TObject);
Var
ini: tinifile;
Begin
// Speichern aller Einstellungen
If SaveDialog1.Execute Then Begin
ini := TIniFile.Create(SaveDialog1.FileName);
ini.WriteString('Heap Properties', 'Strategy', ComboBox1.text);
ini.WriteString('Heap Properties', 'Size', Edit1.text);
ini.WriteString('Stress scenarios', 'Repeat', edit2.text);
ini.WriteString('Stress scenarios', 'Seed', edit10.text);
If RadioButton1.Checked Then Begin
ini.Writeinteger('Stress scenarios', 'Methode', 0);
End
Else Begin
If RadioButton2.Checked Then Begin
ini.Writeinteger('Stress scenarios', 'Methode', 1);
End
Else Begin
ini.Writeinteger('Stress scenarios', 'Methode', 2);
End;
End;
ini.WriteString('Random blocks', 'Min', edit3.text);
ini.WriteString('Random blocks', 'Max', edit4.text);
ini.WriteString('Random blocks', 'Min livetime', edit8.text);
ini.WriteString('Random blocks', 'Max livetime', edit9.text);
ini.WriteString('Array sim', 'Min', edit5.text);
ini.WriteString('Array sim', 'Max', edit6.text);
ini.WriteString('Array sim', 'Size', edit7.text);
ini.WriteString('Alternating alloc', 'List', edit13.text);
ini.WriteString('Alternating alloc', 'Min livetime', edit11.text);
ini.WriteString('Alternating alloc', 'Max livetime', edit12.text);
ini.WriteString('Plot settings', 'Name', edit14.text);
ini.WriteBool('Plot settings', 'Use filling', CheckBox4.Checked);
ini.WriteString('Plot settings', 'filling Color', ColorToString(ColorBox4.Selected));
ini.WriteBool('Plot settings', 'Use clipping', CheckBox7.Checked);
ini.WriteString('Plot settings', 'clipping Color', ColorToString(ColorBox5.Selected));
ini.WriteBool('Plot settings', 'Use absolute', CheckBox3.Checked);
ini.WriteString('Plot settings', 'absolute Color', ColorToString(ColorBox1.Selected));
ini.WriteBool('Plot settings', 'Use getmem', CheckBox5.Checked);
ini.WriteString('Plot settings', 'getmem Color', ColorToString(ColorBox2.Selected));
ini.WriteBool('Plot settings', 'Use freemem', CheckBox6.Checked);
ini.WriteString('Plot settings', 'freemem Color', ColorToString(ColorBox3.Selected));
ini.WriteBool('Plot settings', 'Use barcolor', CheckBox8.Checked);
ini.WriteString('Plot settings', 'bar Color', ColorToString(ColorBox6.Selected));
ini.WriteBool('Plot settings', 'Use sum', CheckBox9.Checked);
ini.WriteString('Plot settings', 'sum Color', ColorToString(ColorBox7.Selected));
ini.free;
End;
End;
Procedure TForm1.Button5Click(Sender: TObject);
Var
ini: tinifile;
i: integer;
Begin
// Laden aller Einstellungen
If OpenDialog1.Execute Then Begin
ini := TIniFile.Create(OpenDialog1.FileName);
ComboBox1.text := ini.ReadString('Heap Properties', 'Strategy', ComboBox1.text);
Edit1.text := ini.ReadString('Heap Properties', 'Size', Edit1.text);
edit2.text := ini.ReadString('Stress scenarios', 'Repeat', edit2.text);
edit10.text := ini.ReadString('Stress scenarios', 'Seed', edit10.text);
i := ini.Readinteger('Stress scenarios', 'Methode', 0);
(FindComponent('RadioButton' + inttostr(i + 1)) As TRadioButton).Checked := true;
edit3.text := ini.ReadString('Random blocks', 'Min', edit3.text);
edit4.text := ini.ReadString('Random blocks', 'Max', edit4.text);
edit8.text := ini.ReadString('Random blocks', 'Min livetime', edit8.text);
edit9.text := ini.ReadString('Random blocks', 'Max livetime', edit9.text);
edit5.text := ini.ReadString('Array sim', 'Min', edit5.text);
edit6.text := ini.ReadString('Array sim', 'Max', edit6.text);
edit7.text := ini.ReadString('Array sim', 'Size', edit7.text);
edit13.text := ini.ReadString('Alternating alloc', 'List', edit13.text);
edit11.text := ini.ReadString('Alternating alloc', 'Min livetime', edit11.text);
edit12.text := ini.ReadString('Alternating alloc', 'Max livetime', edit12.text);
edit14.text := ini.ReadString('Plot settings', 'Name', edit14.text);
CheckBox4.Checked := ini.ReadBool('Plot settings', 'Use filling', CheckBox4.Checked);
ColorBox4.Selected := StringToColor(ini.ReadString('Plot settings', 'filling Color', ColorToString(ColorBox4.Selected)));
CheckBox7.Checked := ini.ReadBool('Plot settings', 'Use clipping', CheckBox7.Checked);
ColorBox5.Selected := StringToColor(ini.ReadString('Plot settings', 'clipping Color', ColorToString(ColorBox5.Selected)));
CheckBox3.Checked := ini.ReadBool('Plot settings', 'Use absolute', CheckBox3.Checked);
ColorBox1.Selected := StringToColor(ini.ReadString('Plot settings', 'absolute Color', ColorToString(ColorBox1.Selected)));
CheckBox5.Checked := ini.ReadBool('Plot settings', 'Use getmem', CheckBox5.Checked);
ColorBox2.Selected := StringToColor(ini.ReadString('Plot settings', 'getmem Color', ColorToString(ColorBox2.Selected)));
CheckBox6.Checked := ini.ReadBool('Plot settings', 'Use freemem', CheckBox6.Checked);
ColorBox3.Selected := StringToColor(ini.ReadString('Plot settings', 'freemem Color', ColorToString(ColorBox3.Selected)));
CheckBox8.Checked := ini.ReadBool('Plot settings', 'Use barcolor', CheckBox8.Checked);
ColorBox6.Selected := StringToColor(ini.ReadString('Plot settings', 'bar Color', ColorToString(ColorBox6.Selected)));
CheckBox9.Checked := ini.ReadBool('Plot settings', 'Use sum', CheckBox9.Checked);
ColorBox7.Selected := StringToColor(ini.ReadString('sum settings', 'bar Color', ColorToString(ColorBox7.Selected)));
ini.free;
End;
End;
Procedure TForm1.Button6Click(Sender: TObject);
Type
TSelector = (sRandomBlocks, sArraySim, sAlternatings);
TBlock = Record
CreateIteration: integer;
DieItaration: integer;
Data: Pointer;
DataLen: Integer;
End;
TStatistikValues = Record
Statcnt: integer;
StatFloat: Extended;
StatInt64: int64;
Statiteration: integer;
End;
Const // Entsprechen den Array Positionen im Statistics Array
GetMemCalls = 0;
MinGetMemtime = 1;
AvgGetMemTime = 2;
MaxGetMemTime = 3;
FreememCalls = 4;
SmallestBlock = 5;
BiggestBlock = 6;
MinFreeMemtime = 7;
AvgFreeMemTime = 8;
MaxFreeMemTime = 9;
MaxAllocated = 10;
MinLifetime = 11;
AvgLifetime = 12;
MaxLifetime = 13;
FirstReorder = 14;
sVerschnitt = 15;
MaxVerschnitt = 16;
MaxUsedBytes = 17;
StatisticCount = 18; // Muss immer der Anzahl der zu loggenden Werte sein.
Var
GetmemTime: TLineSeries;
FreememTime: TLineSeries;
Verschnitt: TLineSeries;
Summe: TLineSeries;
Statistics: Array Of TStatistikValues;
AktualAllocated: int64;
AlocBlockInfo: Array Of Record
Len: integer;
Count: integer;
End;
Procedure IncAlocBlockInfo(Len: integer);
Var
i: Integer;
j: Integer;
Begin
If Not CheckBox8.Checked Then exit;
For i := 0 To high(AlocBlockInfo) Do Begin
// Gefunden erhöhen und Raus
If AlocBlockInfo[i].Len = len Then Begin
AlocBlockInfo[i].Count := AlocBlockInfo[i].Count + 1;
exit;
End;
If AlocBlockInfo[i].Len < len Then Begin
// Unseren Block gibt es nicht, er müsste aber Sortiertechnisch vor index i sein.
setlength(AlocBlockInfo, high(AlocBlockInfo) + 2);
For j := High(AlocBlockInfo) - 1 Downto i Do Begin
AlocBlockInfo[j + 1] := AlocBlockInfo[j];
End;
AlocBlockInfo[i].Count := 1;
AlocBlockInfo[i].Len := Len;
exit;
End;
End;
setlength(AlocBlockInfo, high(AlocBlockInfo) + 2);
AlocBlockInfo[high(AlocBlockInfo)].Count := 1;
AlocBlockInfo[high(AlocBlockInfo)].Len := len;
End;
Var
dt, StartTime: int64;
Function GetBlock(Iteration, Len, Livetime: integer): TBlock;
Var
i: integer;
p: PByte;
t: Double;
Begin
If panik Then exit;
IncAlocBlockInfo(len);
Statistics[GetMemCalls].StatInt64 := Statistics[GetMemCalls].StatInt64 + 1;
result.CreateIteration := Iteration;
result.DieItaration := Iteration + Livetime;
result.DataLen := len;
EpikTimer1.Clear();
EpikTimer1.Start();
result.data := heap.GetMem(len);
EpikTimer1.Stop();
If Not assigned(result.data) Then Begin
dt := GetTickCount64 - StartTime;
showmessage(format('Showmessage, error could not allocate %d bytes in iteration %d. Heap Full!', [len, Iteration]));
panik := true;
exit;
End;
AktualAllocated := AktualAllocated + len;
If Statistics[MaxAllocated].StatInt64 < AktualAllocated Then Begin
Statistics[MaxAllocated].StatInt64 := AktualAllocated;
Statistics[MaxAllocated].Statiteration := Iteration;
End;
t := EpikTimer1.Elapsed() * 1000000; // in µs
If assigned(GetmemTime) Then
GetmemTime.AddXY(Iteration, t);
i := Heap.MemSize(result.Data) - Result.DataLen;
Statistics[sVerschnitt].StatInt64 := Statistics[sVerschnitt].StatInt64 + i;
If Statistics[MaxVerschnitt].StatInt64 < Statistics[sVerschnitt].StatInt64 Then Begin
Statistics[MaxVerschnitt].StatInt64 := Statistics[sVerschnitt].StatInt64;
Statistics[MaxVerschnitt].Statiteration := Iteration;
End;
If (Statistics[MinGetMemtime].Statiteration = -1) Or (t < Statistics[MinGetMemtime].StatFloat) Then Begin
Statistics[MinGetMemtime].StatFloat := t;
Statistics[MinGetMemtime].Statiteration := Iteration;
End;
If (Statistics[MaxGetMemtime].Statiteration = -1) Or (t > Statistics[MaxGetMemtime].StatFloat) Then Begin
Statistics[MaxGetMemtime].StatFloat := t;
Statistics[MaxGetMemtime].Statiteration := Iteration;
End;
If (Statistics[SmallestBlock].Statiteration = -1) Or (len < Statistics[SmallestBlock].StatInt64) Then Begin
Statistics[SmallestBlock].StatInt64 := len;
Statistics[SmallestBlock].Statiteration := Iteration;
End;
If (Statistics[BiggestBlock].Statiteration = -1) Or (len > Statistics[BiggestBlock].StatInt64) Then Begin
Statistics[BiggestBlock].StatInt64 := len;
Statistics[BiggestBlock].Statiteration := Iteration;
End;
Statistics[AvgGetMemTime].StatInt64 := Statistics[AvgGetMemTime].StatInt64 + 1;
Statistics[AvgGetMemTime].StatFloat := Statistics[AvgGetMemTime].StatFloat + t;
If (Statistics[MinLifetime].Statiteration = -1) Or (Livetime < Statistics[MinLifetime].StatInt64) Then Begin
Statistics[MinLifetime].StatInt64 := Livetime;
Statistics[MinLifetime].Statiteration := Iteration;
End;
If (Statistics[MaxLifetime].Statiteration = -1) Or (Livetime > Statistics[MaxLifetime].StatInt64) Then Begin
Statistics[MaxLifetime].StatInt64 := Livetime;
Statistics[MaxLifetime].Statiteration := Iteration;
End;
Statistics[AvgLifetime].StatInt64 := Statistics[AvgLifetime].StatInt64 + Livetime;
Statistics[AvgLifetime].Statcnt := Statistics[AvgLifetime].Statcnt + 1;
p := result.Data;
For i := 0 To len - 1 Do Begin
p^ := (Iteration Mod 256);
inc(p);
End;
End;
Procedure FreeBlock(Const Block: TBlock);
Var
i: integer;
p: PByte;
t: Double;
Begin
If panik Then exit;
i := heap.MemSize(block.Data) - Block.DataLen;
Statistics[sVerschnitt].StatInt64 := Statistics[sVerschnitt].StatInt64 - i;
Statistics[FreememCalls].StatInt64 := Statistics[FreememCalls].StatInt64 + 1;
AktualAllocated := AktualAllocated - Block.DataLen;
// Prüfen ob der Speicher nicht Fälschlicherweise überschrieben wurde
p := Block.Data;
For i := 0 To Block.DataLen - 1 Do Begin
If p^ <> (Block.CreateIteration Mod 256) Then Begin
Raise Exception.Create(format('Fatal error, heap cell should be %d but is %d.', [block.CreateIteration, p^]));
End;
inc(p);
End;
EpikTimer1.Clear();
EpikTimer1.Start();
i := heap.FreeMem(Block.Data);
EpikTimer1.Stop();
If i <> 0 Then Begin
showmessage('Error, while freemem call, iteration : ' + inttostr(Block.DieItaration) + ' will abort now.');
panik := true;
End;
t := EpikTimer1.Elapsed() * 1000000; // in µs
If assigned(FreememTime) Then
FreememTime.AddXY(Block.DieItaration, t);
If (Statistics[MinFreeMemtime].Statiteration = -1) Or (t < Statistics[MinFreeMemtime].StatFloat) Then Begin
Statistics[MinFreeMemtime].StatFloat := t;
Statistics[MinFreeMemtime].Statiteration := Block.DieItaration;
End;
If (Statistics[MaxFreeMemTime].Statiteration = -1) Or (t > Statistics[MaxFreeMemTime].StatFloat) Then Begin
Statistics[MaxFreeMemTime].StatFloat := t;
Statistics[MaxFreeMemTime].Statiteration := Block.DieItaration;
End;
Statistics[AvgFreeMemTime].StatInt64 := Statistics[AvgFreeMemTime].StatInt64 + 1;
Statistics[AvgFreeMemTime].StatFloat := Statistics[AvgFreeMemTime].StatFloat + t;
End;
Var
Blocks: Array Of TBlock;
Procedure SortBlocks(li, re: integer);
Var
l, r, p: Integer;
h: TBlock;
Begin
If Li < Re Then Begin
p := blocks[Trunc((li + re) / 2)].DieItaration; // Auslesen des Pivo Elementes
l := Li;
r := re;
While l < r Do Begin
While blocks[l].DieItaration < p Do
inc(l);
While blocks[r].DieItaration > p Do
dec(r);
If L <= R Then Begin
h := blocks[l];
blocks[l] := blocks[r];
blocks[r] := h;
inc(l);
dec(r);
End;
End;
SortBlocks(li, r);
SortBlocks(l, re);
End;
End;
Var
ReportLines: Array Of Record
Text, value: String;
End;
ReportLinesCnt: integer;
Procedure AddReportLine(Text, Value: String);
Begin
ReportLines[ReportLinesCnt].Text := Text;
ReportLines[ReportLinesCnt].value := Value;
inc(ReportLinesCnt);
If ReportLinesCnt > high(ReportLines) Then Begin
setlength(ReportLines, high(ReportLines) + 101);
End;
End;
Procedure CreateReport();
Var
i: integer;
lt, lv: integer;
p, s: String;
Begin
lt := 0;
lv := 0;
For i := 0 To ReportLinesCnt - 1 Do Begin
lt := max(lt, utf8Length(ReportLines[i].Text));
lv := max(lv, utf8length(ReportLines[i].value));
End;
form2.memo1.Lines.BeginUpdate;
form2.Memo1.Clear;
For i := 0 To ReportLinesCnt - 1 Do Begin
// So Setzen, das zwischen dem Längsten Text und Value mindestens 5 Zeichen sind
// Text ist dabei immer Linksbündig und p Rechtsbündig.
s := ReportLines[i].Text;
p := ReportLines[i].value;
While utf8length(s) + utf8length(p) < lt + lv + 5 Do
s := s + ' ';
s := s + p;
form2.memo1.Lines.Add(s);
End;
form2.memo1.Lines.EndUpdate;
End;
Var
n, i, ii: integer;
Selector: TSelector;
// Variablen für Random Blocks
MinSize, MaxSize, Size: integer;
MinLiveTime, MaxLiveTime: Integer;
sizes: Array Of Record
size: Integer;
MinLiveTime, MaxLiveTime: Integer;
End;
sizesptr: integer;
s: String;
chseed, j: Integer;
Allocated, AbsoluteTime: TLineSeries;
k: Integer;
bs: TBarSeries;
sa: TStringArray;
Begin
// Disable LCL
GroupBox1.Enabled := false;
GroupBox2.Enabled := false;
GroupBox7.Enabled := false;
Button6.Enabled := false;
ComboBox2.Enabled := false;
Button7.Enabled := true;
Button10.Enabled := false;
CheckBox10.Enabled := false;
Application.ProcessMessages;
// Eigentliche Abarbeitung
// 1. Zufall initialisieren
If trim(edit10.text) = '0' Then Begin
Randomize;
chseed := Random(high(Integer) - 1) + 1;
End
Else Begin
chseed := strtoint(edit10.text);
End;
RandSeed := chseed;
n := strtointdef(edit2.text, 0);
Panik := false;
EpikTimer1.CalibrateCallOverheads(EpikTimer1.SelectedTimebase^);
EpikTimer1.CalibrateTickFrequency(EpikTimer1.SelectedTimebase^);
Selector := sRandomBlocks;
If RadioButton2.Checked Then Selector := sArraySim;
If RadioButton3.Checked Then Selector := sAlternatings;
blocks := Nil;
sizes := Nil;
setlength(ReportLines, 100);
ReportLinesCnt := 0;
Statistics := Nil;
setlength(Statistics, StatisticCount);
Statistics[GetmemCalls].StatInt64 := 0;
Statistics[FreememCalls].StatInt64 := 0;
AktualAllocated := 0;
Statistics[MaxAllocated].StatInt64 := 0;
Statistics[MaxAllocated].Statiteration := 0;
Statistics[MinGetMemtime].Statiteration := -1;
Statistics[MaxGetMemTime].Statiteration := -1;
Statistics[MinFreeMemtime].Statiteration := -1;
Statistics[MaxFreeMemTime].Statiteration := -1;
Statistics[AvgGetMemTime].StatInt64 := 0;
Statistics[AvgGetMemTime].StatFloat := 0;
Statistics[AvgFreeMemTime].StatInt64 := 0;
Statistics[AvgFreeMemTime].StatFloat := 0;
Statistics[SmallestBlock].Statiteration := -1;
Statistics[BiggestBlock].Statiteration := -1;
Statistics[MinLifetime].Statiteration := -1;
Statistics[AvgLifetime].StatInt64 := 0;
Statistics[AvgLifetime].Statcnt := 0;
Statistics[MaxLifetime].Statiteration := -1;
Statistics[FirstReorder].Statiteration := -1;
Statistics[sVerschnitt].StatInt64 := 0;
Statistics[MaxVerschnitt].StatInt64 := 0;
Statistics[MaxUsedBytes].StatInt64 := 0;
setlength(AlocBlockInfo, 0);
Case Selector Of
sRandomBlocks: Begin
MinSize := strtointdef(edit3.text, -1);
MaxSize := strtointdef(edit4.text, -1);
MinLiveTime := strtointdef(edit8.text, -1);
MaxLiveTime := strtointdef(edit9.text, -1);
// Fehlerhandling, Range Checks
If (MinSize <= 0) Or (MaxSize < MinSize) Then Begin
showmessage('Error invalid size configuration.');
exit;
End;
If (MinLiveTime <= 0) Or (MaxLiveTime < MinLiveTime) Then Begin
showmessage('Error invalid livetime configuration.');
exit;
End;
End;
sArraySim: Begin
MinSize := strtoint(edit5.text);
MaxSize := strtoint(edit6.text);
Size := strtoint(edit7.text);
// Fehlerhandling, Range Checks
If (MinSize <= 0) Or (MaxSize < MinSize) Then Begin
showmessage('Error invalid length configuration.');
exit;
End;
If (Size <= 0) Then Begin
showmessage('Error invalid size configuration.');
exit;
End;
End;
sAlternatings: Begin
s := Edit13.Text + ',';
While trim(s) <> '' Do Begin
SetLength(sizes, high(sizes) + 2);
sizes[high(sizes)].size := strtointdef(trim(copy(s, 1, pos(',', s) - 1)), -1);
If sizes[high(sizes)].size <= 0 Then Begin
showmessage('Error invalid size configuration.');
SetLength(sizes, 0);
exit;
End;
delete(s, 1, pos(',', s));
End;
If pos(',', edit11.text) <> 0 Then Begin
s := edit11.text;
sa := s.split(',');
If length(sa) <> length(sizes) Then Begin
showmessage('Error invalid minvalues count configuration.');
SetLength(sizes, 0);
exit;
End;
For i := 0 To high(sa) Do Begin
sizes[i].MinLiveTime := strtoint(trim(sa[i]));
End;
End
Else Begin
MinLiveTime := strtoint(edit11.text);
For i := 0 To high(sizes) Do Begin
sizes[i].MinLiveTime := MinLiveTime;
End;
End;
If pos(',', edit12.text) <> 0 Then Begin
s := edit12.text;
sa := s.split(',');
If length(sa) <> length(sizes) Then Begin
showmessage('Error invalid maxvalues count configuration.');
SetLength(sizes, 0);
exit;
End;
For i := 0 To high(sa) Do Begin
sizes[i].MaxLiveTime := strtoint(trim(sa[i]));
End;
End
Else Begin
MaxLiveTime := strtoint(edit12.text);
For i := 0 To high(sizes) Do Begin
sizes[i].MaxLiveTime := MinLiveTime;
End;
End;
For i := 0 To high(sizes) Do Begin
If (sizes[i].MinLiveTime <= 0) Or (sizes[i].MaxLiveTime < sizes[i].MinLiveTime) Then Begin
showmessage('Error invalid livetime configuration.');
exit;
End;
End;
sizesptr := 0;
End;
End;
If Not CheckBox10.Checked Then Begin
Chart1.DisableRedrawing;
Chart2.DisableRedrawing;
Chart3.DisableRedrawing;
Chart4.DisableRedrawing;
End;
If CheckBox3.Checked Then Begin
AbsoluteTime := TLineSeries.Create(Chart1);
AbsoluteTime.Title := 'Time ' + Edit14.text;
AbsoluteTime.SeriesColor := ColorBox1.Selected;
AbsoluteTime.Pointer.Brush.Color := ColorBox1.Selected;
AbsoluteTime.AxisIndexX := 1;
Chart1.AddSeries(AbsoluteTime);
AbsoluteTime.AddXY(0, 0);
End
Else
AbsoluteTime := Nil;
If CheckBox4.Checked Then Begin
Allocated := TLineSeries.Create(Chart3);
Allocated.Title := 'Alloc ' + Edit14.text;
Allocated.SeriesColor := ColorBox4.Selected;
Allocated.Pointer.Brush.Color := ColorBox4.Selected;
Chart3.AddSeries(Allocated);
Allocated.AddXY(0, 0);
End
Else
Allocated := Nil;
If CheckBox9.Checked Then Begin
Summe := TLineSeries.Create(Chart3);
Summe.Title := 'Sum ' + Edit14.text;
Summe.SeriesColor := ColorBox7.Selected;
Summe.Pointer.Brush.Color := ColorBox7.Selected;
Chart3.AddSeries(Summe);
Summe.AddXY(0, 0);
End
Else
Summe := Nil;
If CheckBox7.Checked Then Begin
Verschnitt := TLineSeries.Create(Chart3);
Verschnitt.Title := 'Clip ' + Edit14.text;
Verschnitt.SeriesColor := ColorBox5.Selected;
Verschnitt.Pointer.Brush.Color := ColorBox5.Selected;
Chart3.AddSeries(Verschnitt);
Verschnitt.AddXY(0, 0);
End
Else
Verschnitt := Nil;
If CheckBox5.Checked Then Begin
GetmemTime := TLineSeries.Create(Chart2);
GetmemTime.Title := 'Getmem ' + Edit14.text;
GetmemTime.SeriesColor := ColorBox2.Selected;
GetmemTime.Pointer.Brush.Color := ColorBox2.Selected;
Chart2.AddSeries(GetmemTime);
End
Else
GetmemTime := Nil;
If CheckBox6.Checked Then Begin
FreememTime := TLineSeries.Create(Chart2);
FreememTime.Title := 'Freemem ' + Edit14.text;
FreememTime.SeriesColor := ColorBox3.Selected;
FreememTime.Pointer.Brush.Color := ColorBox3.Selected;
Chart2.AddSeries(FreememTime);
End
Else
FreememTime := Nil;
If CheckBox8.Checked Then Begin
bs := TBarSeries.Create(Chart2);
bs.Title := 'Blocks ' + Edit14.text;
bs.SeriesColor := ColorBox6.Selected;
bs.BarBrush.Color := ColorBox6.Selected;
Chart4.AddSeries(bs);
End;
CheckBox2Change(Nil); // Anpassen des Styles der Kurven
heap.ReCreate();
ii := 0;
StartTime := gettickcount64;
// Die Eigentliche Durchführung des Stests
For i := 0 To n - 1 Do Begin
Case Selector Of
sRandomBlocks: Begin
setlength(Blocks, high(Blocks) + 2);
blocks[high(Blocks)] := GetBlock(i, RandomRange(MinSize, MaxSize), RandomRange(MinLiveTime, MaxLiveTime));
End;
sArraySim: Begin
// Über die Lebenszeit werden die Blocks automatisch gelöscht ;)
setlength(Blocks, high(Blocks) + 2);
blocks[high(Blocks)] := GetBlock(i, (MinSize + (i Mod (MaxSize - MinSize + 1))) * Size, 1);
End;
sAlternatings: Begin
setlength(Blocks, high(Blocks) + 2);
blocks[high(Blocks)] := GetBlock(i, sizes[sizesptr].size, RandomRange(sizes[sizesptr].MinLiveTime, sizes[sizesptr].MaxLiveTime));
sizesptr := (sizesptr + 1) Mod length(sizes);
End;
End;
// Das muss vor dem Freigeben Gemessen werden, da der Freigabevorgang diese Werte ja wieder verringert.
If statistics[sVerschnitt].StatInt64 + AktualAllocated > Statistics[MaxUsedBytes].StatInt64 Then Begin
Statistics[MaxUsedBytes].StatInt64 := statistics[sVerschnitt].StatInt64 + AktualAllocated;
Statistics[MaxUsedBytes].Statiteration := ii;
End;
j := 0;
While j <= high(blocks) Do Begin
If i >= blocks[j].DieItaration Then Begin
FreeBlock(Blocks[j]);
For k := j To high(Blocks) - 1 Do Begin
Blocks[k] := Blocks[k + 1];
End;
setlength(blocks, high(blocks));
dec(j);
End;
inc(j);
End;
ii := i + 1;
// Die Absolut Zeit in ms
If (i Mod 1000) = 999 Then Begin
dt := GetTickCount64 - StartTime;
// X- Achse = Anzahl der Durchläufe
// Y- Achse = Vergange Zeit in s
If assigned(AbsoluteTime) Then
AbsoluteTime.AddXY((ii), dt / 1000);
End;
If assigned(Allocated) Then
Allocated.AddXY(ii, (AktualAllocated * 100) / heap.HeapSize);
If assigned(Verschnitt) Then
Verschnitt.AddXY(ii, (statistics[sVerschnitt].StatInt64 * 100) / heap.HeapSize);
If assigned(summe) Then
Summe.AddXY(ii, ((statistics[sVerschnitt].StatInt64 + AktualAllocated) * 100) / heap.HeapSize);
// Merken, wann zum ersten Mal die Freispeicherliste neu Organisiert wurde
If Statistics[FirstReorder].Statiteration = -1 Then Begin
If heap.FreeListReorganisations > 0 Then Begin
Statistics[FirstReorder].Statiteration := ii;