summaryrefslogtreecommitdiff
path: root/tests/src/GC/Stress/Tests/ThdTreeGrowingObj.cs
blob: 01c17c0d1db2072b389f4e2e25a059744d7bff17 (plain)
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


using System.Threading;
using System;
using System.IO;
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

/**
 * Description:
 *      Mainly stresses the GC by creating n threads each manipulating its own local binary tree.
 *      Differs from thdtree in a way that the nodes of the binary trees grow during the lifetime.
 */



namespace DefaultNamespace
{
    public enum TreeType
    {
        Normal,
        Growing,
        Living
    }

    public class Node
    {
        internal int m_data;
        internal Node m_pLeft;
        internal Node m_pRight;
        internal byte[] m_aMem;
        internal bool Switch;
        internal int m_iCount;

        public Node()
        {
            m_aMem = new byte[10];
            m_aMem[0] = (byte)10;
            m_aMem[9] = (byte)10;
        }

        public void Live()
        {
            if (Switch)
            {
                m_aMem = new byte[1000];
                m_aMem[0] = (byte)10;
                m_aMem[999] = (byte)10;
            }
            else
            {
                m_aMem = new byte[10];
                m_aMem[0] = (byte)10;
                m_aMem[9] = (byte)10;
            }

            Switch = !Switch;
        }

        public void Grow()
        {
            m_aMem = new byte[(m_iCount += 100)];
            m_aMem[0] = (byte)10;
            m_aMem[m_iCount - 1] = (byte)10;
        }
    }


    public class BinTree
    {
        internal Node m_pRoot;
        internal Random m_Random;
        internal TreeType m_TreeType;

        public BinTree(int ThreadId, TreeType treeType)
        {
            m_TreeType = treeType;
            m_pRoot = null;
            m_Random = new Random();
        }


        public void Empty(int ThreadId)
        {
            Console.Out.WriteLine("Thread " + ThreadId + ": Tree Empty");
            m_pRoot = null;
        }


        public void AddNodes(int howMany, int ThreadId)
        {
            for (int i = 0; i < howMany; i++)
            {
                m_pRoot = Insert(m_pRoot, m_Random.Next(100));
            }
            Console.Out.WriteLine("Thread " + ThreadId + " Added: " + howMany + " Nodes: " + GC.GetTotalMemory(false));
        }


        public void DeleteNodes(int howMany, int ThreadId)
        {
            for (int i = 0; i < howMany; i++)
            {
                m_pRoot = Delete(m_pRoot, m_Random.Next(100));
            }
            Console.Out.WriteLine("Thread " + ThreadId + " Deleted: " + howMany + " Nodes: " + GC.GetTotalMemory(false));
        }


        public Node Insert(Node root, int element)
        {
            if (root == null)                                            //if is NULL make a new node
            {                                                           //and copy number to the new node
                root = new Node();                                        //make new node
                root.m_data = element;                                  //copy number
                root.m_pLeft = null;                                     //set the children to NULL
                root.m_pRight = null;
            }
            else if (element < root.m_data)
            {
                root.m_pLeft = Insert(root.m_pLeft, element);
            }
            else
            {
                root.m_pRight = Insert(root.m_pRight, element);
            }

            if (m_TreeType == TreeType.Growing)
            {
                root.Grow();
            }
            else if (m_TreeType == TreeType.Living)
            {
                root.Live();
            }

            return root;
        }


        public Node Delete(Node root, int element)
        {
            Node temp = null;

            if (root == null)
            {
                return null;                                                //Node not found
            }
            else if (element == root.m_data)                                 //if it was the first data (node)
            {
                if (root.m_pRight == null)                                       //check if it has right child.
                {                                                           //If it has no right child
                    return root.m_pLeft;
                }

                if (root.m_pLeft == null)
                {
                    return root.m_pRight;
                }
                else
                {
                    for (temp = root.m_pLeft; temp.m_pRight != null; temp = temp.m_pRight) ;
                    root.m_data = temp.m_data;
                    root.m_pLeft = Delete(root.m_pLeft, temp.m_data);
                }
            }
            else if (root.m_data > element)
            {
                root.m_pLeft = Delete(root.m_pLeft, element);
            }
            else
            {
                root.m_pRight = Delete(root.m_pRight, element);
            }

            if (m_TreeType == TreeType.Growing)
            {
                root.Grow();
            }
            else if (m_TreeType == TreeType.Living)
            {
                root.Live();
            }

            return root;
        }
    }

    public class TreeThread
    {
        internal int[] mA_Count;
        internal int m_id = 0;
        internal BinTree m_BinTree;
        internal Thread Mv_Thread;

        public TreeThread(int ThreadId, TreeType treeType, int[] count)
        {
            mA_Count = count;
            m_BinTree = new BinTree(ThreadId, treeType);
            m_id = ThreadId;
            Mv_Thread = new Thread(new ThreadStart(this.ThreadStart));
            Mv_Thread.Start();
            Console.Out.WriteLine("Started Thread: " + m_id);
        }

        public void ThreadStart()
        {                                           //All threads start here
            for (int i = 0; i < mA_Count.Length; i++)
            {
                if (mA_Count[i] == 0)
                {
                    m_BinTree.Empty(m_id);
                }
                else if (mA_Count[i] > 0)
                {
                    m_BinTree.AddNodes(mA_Count[i], m_id);
                }
                else
                {
                    m_BinTree.DeleteNodes((mA_Count[i] * -1), m_id);
                }
            }
        }
    }

    public class ThdTreeGrowingObj
    {
        public static int Main(System.String[] Args)
        {
            int iNofThread = 0;

            if (Args.Length == 1)
            {
                if (!Int32.TryParse(Args[0], out iNofThread))
                {
                    iNofThread = 2;
                }
            }
            else
            {
                iNofThread = 2;
            }

            int[] count = { 300, 1000, -350, 0, 71, 200 };
            TreeThread Mv_TreeThread;
            for (int i = 0; i < iNofThread; i++)
            {
                Mv_TreeThread = new TreeThread(i, TreeType.Growing, count);              //Each treethread object launches a thread
            }
            return 100;
        }
    }
}