Reduce TLS accesses. (#11487)
[mono-project.git] / mono / tests / gchandle-stress.cs
blob382caf20ce6c455afa6e24ac127cc6ebcc201e36
1 using System;
2 using System.Runtime.InteropServices;
4 // in this test we spend
5 // 30% of the time locking
6 // 10 % allocating the handles
7 class T {
9 static GCHandle[] handle_array;
11 static int count = 4 * 400000; /* multiple of handle types */
12 static int loops = 2;
14 static void build_array () {
15 int i;
16 handle_array = new GCHandle [count];
18 for (i = 0; i < count; ++i) {
19 GCHandleType t = (GCHandleType) (i & 3);
20 handle_array [i] = GCHandle.Alloc (i, t);
23 static void get_stats (){
24 int i;
25 object o;
26 int has_target = 0;
27 int is_allocated = 0;
28 int normal_reclaimed = 0;
29 for (i = 0; i < count; ++i) {
30 GCHandleType t = (GCHandleType) (i & 3);
31 if (handle_array [i].IsAllocated)
32 is_allocated++;
33 else
34 continue;
35 o = handle_array [i].Target;
36 if (o != null) {
37 has_target++;
38 int val = (int)o;
39 if (val != i)
40 Console.WriteLine ("obj at {0} inconsistent: {1}", i, val);
41 } else {
42 if (t == GCHandleType.Normal || t == GCHandleType.Pinned) {
43 normal_reclaimed++;
47 Console.WriteLine ("allocated: {0}, has target: {1}, normal reclaimed: {2}", is_allocated, has_target, normal_reclaimed);
50 static void free_some (int d) {
51 int i;
52 int freed = 0;
53 for (i = 0; i < count; ++i) {
54 if ((i % d) == 0) {
55 if (handle_array [i].IsAllocated) {
56 handle_array [i].Free ();
57 freed++;
61 Console.WriteLine ("freed: {0}", freed);
64 static void alloc_many () {
65 int small_count = count / 2;
66 GCHandle[] more = new GCHandle [small_count];
67 int i;
68 for (i = 0; i < small_count; ++i) {
69 GCHandleType t = (GCHandleType) (i & 3);
70 more [i] = GCHandle.Alloc (i, t);
72 for (i = 0; i < small_count; ++i) {
73 more [i].Free ();
75 Console.WriteLine ("alloc many: {0}", small_count);
78 static void Main (string[] args) {
79 if (args.Length > 0)
80 count = 4 * int.Parse (args [0]);
81 if (args.Length > 1)
82 loops = int.Parse (args [1]);
84 for (int j = 0; j < loops; ++j) {
85 do_one ();
89 static void do_one () {
90 Console.WriteLine ("start");
91 build_array ();
92 get_stats ();
93 GC.Collect ();
94 Console.WriteLine ("after collect");
95 get_stats ();
96 free_some (10);
97 Console.WriteLine ("after free(10)");
98 get_stats ();
99 free_some (4);
100 Console.WriteLine ("after free(4)");
101 get_stats ();
102 GC.Collect ();
103 Console.WriteLine ("after collect");
104 get_stats ();
105 for (int i = 0; i < 10; ++i)
106 alloc_many ();
107 Console.WriteLine ("after alloc_many");
108 get_stats ();
109 free_some (1);
110 Console.WriteLine ("after free all");
111 get_stats ();
112 GC.Collect ();
113 Console.WriteLine ("after collect");
114 get_stats ();