Merge pull request #5406 from kumpera/fix_12157
[mono.git] / mono / tests / gc-graystack-stress.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4
5 class Program {
6         
7         class Node {
8                 Node child;
9                 Node sibling;
10
11                 public Node (int depth) : this (depth, null) {}
12
13                 public Node (int depth, Node sibling) {
14                         if (depth > 0)
15                                 this.child = new Node (depth - 1);
16
17                         this.sibling = sibling;
18                 }
19
20                 public override String ToString () {
21                         return String.Format ("Node[child={0},sibling={1}]", this.child, this.sibling);
22                 }
23         }
24         
25         /**
26          * Usage : width [depth [collections]]
27          *  - width : trigger the overflow
28          *  - depth : modify the cost difference of the overflow
29          *  - collections : # of collections to perform
30          */
31         public static void Main (String[] args) {
32                 int width = 125;
33                 if (args.Length > 0)
34                         width = Math.Max (width, Int32.Parse (args [0]));
35
36                 // Windows x64 only has 1 MB of stack per thread which is less than other x86 64-bit OSes.
37                 // Using 10000 for depth will cause a stack overflow on Windows x64. 5000 will fit.
38                 int platform = (int) Environment.OSVersion.Platform;
39                 bool isWin64 = !(platform == 4 || platform == 128) && Environment.Is64BitProcess;
40                 int depth = isWin64 ? 5000 : 10000;
41                 if (args.Length > 1)
42                         depth = Math.Max (depth, Int32.Parse (args [1]));
43
44                 int collections = 100;
45                 if (args.Length > 2)
46                         collections = Math.Max (collections, Int32.Parse (args [2]));
47
48                 Node sibling = null;
49
50                 for (int i = 0; i < width; i++) {
51                         sibling = new Node(depth, sibling);
52                         if (i > 0 && i % 10 == 0)
53                                 Console.Write ("+");
54                 }
55
56                 for (int i = 0; i < collections; i++) {
57                         GC.Collect();
58                         if (i > 0 && i % 10 == 0)
59                                 Console.Write (".");
60                 }
61                 Console.WriteLine ();
62         }
63 }