source: trunk/Utilities/Miscellaneous/AllocateCost/AllocateCost.cpp @ 127

Last change on this file since 127 was 127, checked in by roman, 11 years ago
  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2012
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: AllocateCost.cpp 127 2012-10-01 19:53:13Z roman $
6
7#include "stdafx.h"
8#include <atlsync.h>
9
10////////////////////////////////////////////////////////////
11// CTraitsA
12
13class CTraitsA
14{
15public:
16
17        ////////////////////////////////////////////////////////
18        // CBlock
19
20        class CBlock
21        {
22        public:
23                CHeapPtr<BYTE> m_pnData;
24
25        public:
26        // CBlock
27                CBlock() throw()
28                {
29                }
30                VOID Reallocate(SIZE_T nDataSize)
31                {
32                        ATLVERIFY(m_pnData.Reallocate(nDataSize));
33                }
34                BYTE* GetData() throw()
35                {
36                        return m_pnData;
37                }
38        };
39
40public:
41// CTraitsA
42};
43
44////////////////////////////////////////////////////////////
45// CTraitsB
46
47class CTraitsB
48{
49public:
50
51        ////////////////////////////////////////////////////////
52        // CBlock
53
54        class CBlock
55        {
56        public:
57                CHeapPtr<BYTE> m_pnData;
58
59        public:
60        // CBlock
61                CBlock() throw()
62                {
63                }
64                VOID Reallocate(SIZE_T nDataSize)
65                {
66                        m_pnData.Free();
67                        ATLVERIFY(m_pnData.Allocate(nDataSize));
68                }
69                BYTE* GetData() throw()
70                {
71                        return m_pnData;
72                }
73        };
74
75public:
76// CTraitsB
77};
78
79////////////////////////////////////////////////////////////
80// CTraitsC
81
82class CTraitsC
83{
84public:
85
86        ////////////////////////////////////////////////////////
87        // CBlock
88
89        class CBlock
90        {
91        public:
92                CHeapPtr<BYTE> m_pnData;
93                SIZE_T m_nDataCapacity;
94                SIZE_T m_nDataSize;
95
96        public:
97        // CBlock
98                CBlock() throw() :
99                        m_nDataCapacity(0)
100                {
101                }
102                VOID Reallocate(SIZE_T nDataSize)
103                {
104                        static const SIZE_T g_nAlignment = 32 << 10; // 32 KB
105                        ATLASSERT(!(g_nAlignment & (g_nAlignment - 1)));
106                        const SIZE_T nDataCapacity = (nDataSize + g_nAlignment - 1) & ~(g_nAlignment - 1);
107                        if(nDataCapacity > m_nDataCapacity || nDataCapacity < (m_nDataCapacity >> 4))
108                        {
109                                ATLVERIFY(m_pnData.Reallocate(nDataCapacity));
110                                m_nDataCapacity = nDataCapacity;
111                        }
112                        ATLASSERT(nDataSize <= m_nDataCapacity);
113                        m_nDataSize = nDataSize;
114                }
115                BYTE* GetData() throw()
116                {
117                        return m_pnData;
118                }
119        };
120
121public:
122// CTraitsC
123};
124
125////////////////////////////////////////////////////////////
126// CTestT
127
128template <typename CTraits>
129class CTestT
130{
131        typedef CTestT<CTraits> CModule;
132
133private:
134        volatile LONG m_nCount;
135        CEvent m_StartEvent;
136        CEvent m_StopEvent;
137        SIZE_T m_nDataSize;
138        CHeapPtr<BYTE> m_pnData;
139
140public:
141// CTestT
142        CTestT()
143        {
144        }
145        DWORD Rally()
146        {
147                const DWORD nWaitResult = WaitForSingleObject(m_StartEvent, INFINITE);
148                ATLASSERT(nWaitResult == WAIT_OBJECT_0);
149                typename CTraits::CBlock Block;
150                for(UINT nIndex = 0; ; nIndex++)
151                {
152                        if(!(nIndex % 1024))
153                                if(WaitForSingleObject(m_StopEvent, 0) == WAIT_OBJECT_0)
154                                        break;
155                        SIZE_T nDataSize = (1 << 10) +  rand() * (m_nDataSize - (1 << 10)) / RAND_MAX;
156                        Block.Reallocate(nDataSize);
157                        memcpy(Block.GetData(), m_pnData, nDataSize);
158                        InterlockedIncrement(&m_nCount);
159                }
160                return 0;
161        }
162        static DWORD __stdcall Rally(CModule* pModule)
163        {
164                return pModule->Rally();
165        }
166        VOID Run()
167        {
168                m_nDataSize = 1 << 20; // 1 MB
169                ATLVERIFY(m_pnData.Allocate(m_nDataSize));
170                for(SIZE_T nIndex = 0; nIndex < m_nDataSize; nIndex++)
171                        m_pnData[nIndex] = (BYTE) rand();
172                m_nCount = 0;
173                ATLVERIFY(m_StartEvent.Create(NULL, TRUE, FALSE, NULL));
174                ATLVERIFY(m_StopEvent.Create(NULL, TRUE, FALSE, NULL));
175                static const SIZE_T g_nThreadCount = 8;
176                CHandle pThreads[g_nThreadCount];
177                for(SIZE_T nThreadIndex = 0; nThreadIndex < g_nThreadCount; nThreadIndex++)
178                {
179                        pThreads[nThreadIndex].Attach(AtlCreateThread<CModule>(&CModule::Rally, this));
180                        ATLASSERT(pThreads[nThreadIndex]);
181                }
182                Sleep(100); // 0.1 second
183                ATLVERIFY(m_StartEvent.Set());
184                Sleep(10 * 1000); // 10 seconds
185                const LONG nCount = InterlockedIncrement(&m_nCount) - 1;
186                ATLVERIFY(m_StopEvent.Set());
187                const DWORD nWaitResult = WaitForMultipleObjects(g_nThreadCount, reinterpret_cast<HANDLE*>(pThreads), TRUE, INFINITE);
188                ATLASSERT(nWaitResult == WAIT_OBJECT_0);
189                _tprintf(_T("%d\n"), nCount);
190        }
191};
192
193int _tmain(int argc, _TCHAR* argv[])
194{
195        srand(1);
196        SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
197        for(SIZE_T nIndex = 0; nIndex < 5; nIndex++)
198        {
199                CTestT<CTraitsA>().Run();
200                CTestT<CTraitsB>().Run();
201                CTestT<CTraitsC>().Run();
202                _tprintf(_T("%\n"));
203        }
204        return 0;
205}
206
Note: See TracBrowser for help on using the repository browser.