source: trunk/Utilities/LoadCpu/LoadCpu.cpp @ 7

Last change on this file since 7 was 5, checked in by roman, 13 years ago
  • Property svn:keywords set to Id
File size: 5.7 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2011
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: LoadCpu.cpp 5 2011-08-25 07:19:48Z roman $
6
7#include "stdafx.h"
8
9////////////////////////////////////////////////////////////
10// CModule
11
12class CModule : 
13        public CAtlExeModuleT<CModule>
14{
15        typedef CThreadT<CModule> CThread;
16
17private:
18        DWORD_PTR m_nAffinityMask;
19        UINT m_nRate;
20        CEvent m_CompletionEvent;
21        ULONG m_nActivityTime;
22        ULONG m_nInactivityTime;
23        CEvent m_ActivityEvent;
24        CRoListT<CObjectPtr<CThread>> m_ThreadList;
25
26public:
27// CModule
28        CModule() throw() :
29                m_nAffinityMask((DWORD) -1),
30                m_nRate(100),
31                m_CompletionEvent(TRUE, FALSE),
32                m_ActivityEvent(TRUE, FALSE)
33        {
34#if defined(_DEBUG)
35                AtlTraceLoadSettings(NULL);
36#endif // defined(_DEBUG)
37                _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
38        }
39        ~CModule() throw()
40        {
41                _Z4(atlTraceRefcount, 4, _T("this 0x%p\n"), this);
42        }
43        VOID SetAffinityMask(DWORD_PTR nAffinityMask)
44        {
45                m_nAffinityMask = nAffinityMask;
46        }
47        VOID SetRate(UINT nRate)
48        {
49                m_nRate = nRate;
50        }
51        DWORD ThreadProc(CThread*, CEvent& InitializationEvent, CEvent& TerminationEvent)
52        {
53                const UINT nRate = m_nRate;
54                _W(InitializationEvent.Set());
55                const HANDLE phObjects[] = { TerminationEvent, m_ActivityEvent };
56                const DWORD nWaitResult = WaitForMultipleObjects(DIM(phObjects), phObjects, FALSE, INFINITE);
57                _Z4(atlTraceSync, 4, _T("nWaitResult 0x%x\n"), nWaitResult);
58                _A(nWaitResult - WAIT_OBJECT_0 < DIM(phObjects));
59                if(nWaitResult == WAIT_OBJECT_0 + 1) // m_ActivityEvent
60                {
61                        for(; ; )
62                        {
63                                const DWORD nWaitResult = WaitForSingleObject(TerminationEvent, m_nInactivityTime);
64                                _Z5(atlTraceSync, 5, _T("nWaitResult 0x%x\n"), nWaitResult);
65                                _A(nWaitResult == WAIT_OBJECT_0 || nWaitResult == WAIT_TIMEOUT);
66                                if(nWaitResult != WAIT_TIMEOUT)
67                                        break;
68                                const ULONG nTime = GetTickCount() + m_nActivityTime;
69                                INT nX = 17, nY = 19;
70                                while((LONG) (GetTickCount() - nTime) < 0)
71                                {
72                                        for(INT nZ = 0; nZ < 1000; nZ++)
73                                        {
74                                                nX = nX * nY;
75                                                nY = (nY << 16) / (nX | 1);
76                                        }
77                                }
78                        }
79                }
80                return 0;
81        }
82        HRESULT Run(INT nShowCommand = SW_HIDE)
83        {
84                #pragma region Affinity Mask
85                DWORD_PTR nProcessAffinityMask, nSystemAffinityMask;
86                __E(GetProcessAffinityMask(GetCurrentProcess(), &nProcessAffinityMask, &nSystemAffinityMask));
87                if((nProcessAffinityMask & m_nAffinityMask) != nProcessAffinityMask)
88                {
89                        __E(SetProcessAffinityMask(GetCurrentProcess(), nProcessAffinityMask & m_nAffinityMask));
90                        __E(GetProcessAffinityMask(GetCurrentProcess(), &nProcessAffinityMask, &nSystemAffinityMask));
91                }
92                _tprintf(_T("System Affinity Mask: 0x%02x\n"), nSystemAffinityMask);
93                _tprintf(_T("Process Affinity Mask: 0x%02x\n"), nProcessAffinityMask);
94                #pragma endregion
95                _tprintf(_T("Rate: %d%%\n"), m_nRate);
96                ULONG nActivityTime = 10 * m_nRate, nInactivityTime = 10 * (100 - m_nRate);
97                while(nActivityTime >= 20 && nInactivityTime >= 20)
98                {
99                        nActivityTime >>= 1;
100                        nInactivityTime >>= 1;
101                }
102#if defined(_DEBUG)
103                _tprintf(_T("Activity/Inactivity Times: %d, %d\n"), nActivityTime, nInactivityTime);
104#endif // defined(_DEBUG)
105                m_nActivityTime = nActivityTime;
106                m_nInactivityTime = nInactivityTime;
107                for(DWORD_PTR nIndex = 1; nIndex; nIndex <<= 1)
108                {
109                        if(!(nProcessAffinityMask & nIndex))
110                                continue;
111                        CObjectPtr<CThread> pThread;
112                        __E(pThread.Construct()->Initialize(this, &CModule::ThreadProc));
113                        SetThreadAffinityMask(pThread->GetHandle(), nIndex);
114                        _W(m_ThreadList.AddTail(pThread));
115                }
116                _W(m_ActivityEvent.Set());
117                const DWORD nWaitResult = WaitForSingleObject(m_CompletionEvent, INFINITE);
118                _Z4(atlTraceSync, 4, _T("nWaitResult 0x%x\n"), nWaitResult);
119                _A(nWaitResult == WAIT_OBJECT_0);
120                m_ThreadList.RemoveAll();
121                return S_OK;
122        }
123};
124
125////////////////////////////////////////////////////////////
126// Main
127
128int _tmain(int argc, _TCHAR* argv[])
129{
130        _ATLTRY
131        {
132                CModule Module;
133                #pragma region Parse Command Line
134                for(INT nIndex = 1; nIndex < argc; nIndex++)
135                {
136                        CString sArgument = argv[nIndex];
137                        _A(!sArgument.IsEmpty());
138                        #pragma region Switches
139                        if(_tcschr(_T("-/"), sArgument[0]))
140                        {
141                                sArgument.Delete(0);
142                                #pragma region Switch Value/Specification
143                                CString sArgumentValue;
144                                if(sArgument.GetLength() > 1)
145                                {
146                                        SIZE_T nIndex = 1;
147                                        if(sArgument[1] == _T(':'))
148                                                nIndex++;
149                                        sArgumentValue = (LPCTSTR) sArgument + nIndex;
150                                }
151                                #pragma endregion
152                                if(_tcschr(_T("Aa"), sArgument[0]))
153                                {
154                                        __D(!sArgumentValue.IsEmpty(), E_INVALIDARG);
155                                        INT nAffinityMask = 0;
156                                        __D(StrToIntEx(sArgumentValue, STIF_SUPPORT_HEX, &nAffinityMask), E_INVALIDARG);
157                                        __D(nAffinityMask, E_INVALIDARG);
158                                        //_tprintf(_T("Affinity: 0x%x\n"), nAffinityMask);
159                                        Module.SetAffinityMask(nAffinityMask);
160                                } else
161                                if(_tcschr(_T("Rr"), sArgument[0]))
162                                {
163                                        __D(!sArgumentValue.IsEmpty(), E_INVALIDARG);
164                                        INT nRate = 0;
165                                        __D(StrToIntEx(sArgumentValue, STIF_SUPPORT_HEX, &nRate), E_INVALIDARG);
166                                        __D(nRate > 0 && nRate < 100, E_INVALIDARG);
167                                        //_tprintf(_T("Rate: %d%%\n"), nRate);
168                                        Module.SetRate(nRate);
169                                }
170                                continue;
171                        }
172                        #pragma endregion
173                        //if(sArgument.GetLength() >= 2 && sArgument[0] == _T('"') && sArgument[sArgument.GetLength() - 1] == _T('"'))
174                        //      sArgument = sArgument.Mid(1, sArgument.GetLength() - 2);
175                        //Module.SetPath(sArgument);
176                }
177                #pragma endregion
178                Module.Run();
179        }
180        _ATLCATCH(Exception)
181        {
182                _tprintf(_T("Error 0x%08x: %s\n"), (HRESULT) Exception, AtlFormatSystemMessage(Exception).TrimRight(_T("\t\n\r .")));
183                return (INT) (HRESULT) Exception;
184        }
185        _ATLCATCHALL()
186        {
187                _tprintf(_T("Fatal Error\n"));
188                return (INT) E_FAIL;
189        }
190        return 0;
191}
Note: See TracBrowser for help on using the repository browser.