Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

610 lines
15 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. //***************************************************************************
  8. //
  9. // PERFCLNT.CPP
  10. //
  11. // WBEM perf counter client sample
  12. //
  13. // raymcc 17-Dec-97 Created.
  14. //
  15. //***************************************************************************
  16. #define _WIN32_WINNT 0x0400
  17. #include "precomp.h"
  18. #include <stdio.h>
  19. #include <locale.h>
  20. #include <wbemidl.h>
  21. #include <wbemint.h>
  22. #include "refresh.h"
  23. //***************************************************************************
  24. //
  25. // Prototypes of internally used functions
  26. //
  27. //***************************************************************************
  28. BOOL Startup(OUT IWbemServices **pRetSvc);
  29. void Menu(IN IWbemServices *pSvc);
  30. void MenuEnumClasses(IN IWbemServices *pSvc);
  31. void MenuEnumInstances(IN IWbemServices *pSvc);
  32. void MenuCreateRefresher();
  33. void MenuDeleteRefresher();
  34. void MenuAddObjectsToRefresher(IN IWbemServices *pSvc);
  35. void MenuRemoveObjectsFromRefresher();
  36. void MenuShowObjectList();
  37. void MenuRefresh();
  38. void MenuDumpObject();
  39. void TightRefreshLoop();
  40. void AddALotOfObjects(IWbemServices *pSvc);
  41. //***************************************************************************
  42. //
  43. // main
  44. //
  45. //***************************************************************************
  46. void main()
  47. {
  48. printf("---WBEM Perf Sample Client---\n");
  49. // Start up by connecting to WBEM.
  50. // ===============================
  51. IWbemServices *pSvc = 0;
  52. BOOL bRes = Startup(&pSvc);
  53. if (bRes == FALSE)
  54. {
  55. printf("Unable to start. Terminating\n");
  56. return;
  57. }
  58. // If here, we are operational.
  59. // ============================
  60. Menu(pSvc);
  61. // Cleanup.
  62. // ========
  63. DestroyRefresher();
  64. pSvc->Release();
  65. CoUninitialize();
  66. }
  67. //***************************************************************************
  68. //
  69. // Menu
  70. //
  71. //***************************************************************************
  72. void Menu(IN IWbemServices *pSvc)
  73. {
  74. while (1)
  75. {
  76. printf(
  77. "\n\n-----------------------------\n"
  78. "99....Quit\n"
  79. "1.....Enumerate classes\n"
  80. "2.....Enumerate instances\n"
  81. "3.....Create the refresher\n"
  82. "4.....Delete the refresher\n"
  83. "5.....Add objects to the refresher\n"
  84. "6.....Remove objects from the refresher\n"
  85. "7.....Refresh!\n"
  86. "8.....Execute tight test loop against the refresher\n"
  87. "9.....List objects in the refresher\n"
  88. "10....Dump an object\n"
  89. "11....Add a lot of objects to the refresher\n"
  90. ">"
  91. );
  92. char buf[32];
  93. int nChoice = atoi(gets(buf));
  94. switch (nChoice)
  95. {
  96. case 99: return;
  97. case 1: MenuEnumClasses(pSvc); break;
  98. case 2: MenuEnumInstances(pSvc); break;
  99. case 3: MenuCreateRefresher(); break;
  100. case 4: MenuDeleteRefresher(); break;
  101. case 5: MenuAddObjectsToRefresher(pSvc); break;
  102. case 6: MenuRemoveObjectsFromRefresher(); break;
  103. case 7: MenuRefresh(); break;
  104. case 8: TightRefreshLoop(); break;
  105. case 9: MenuShowObjectList(); break;
  106. case 10: MenuDumpObject(); break;
  107. case 11: AddALotOfObjects(pSvc); break;
  108. }
  109. }
  110. }
  111. //***************************************************************************
  112. //
  113. // Startup
  114. //
  115. // Logs in to WBEM and returns an active IWbemServices pointer.
  116. //
  117. //***************************************************************************
  118. BOOL Startup(OUT IWbemServices **pRetSvc)
  119. {
  120. CoInitializeEx(0, COINIT_MULTITHREADED);
  121. printf("Locale info = %s\n", setlocale(LC_ALL, ""));
  122. *pRetSvc = 0;
  123. // Try to bring up the locator.
  124. // ============================
  125. IWbemLocator *pLoc = 0;
  126. DWORD dwRes = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
  127. IID_IWbemLocator, (LPVOID *) &pLoc);
  128. if (dwRes != S_OK)
  129. {
  130. printf("Failed to create IWbemLocator object.\n");
  131. CoUninitialize();
  132. return FALSE;
  133. }
  134. // Connect.
  135. // ========
  136. IWbemServices *pSvc = 0;
  137. BSTR Ns = SysAllocString(L"\\\\.\\ROOT\\DEFAULT");
  138. HRESULT hRes = pLoc->ConnectServer(
  139. Ns, // NT perf data namespace
  140. NULL, // User
  141. NULL, // Password
  142. 0, // Locale
  143. 0, // Authentication type
  144. 0, // Authority
  145. 0, // Context
  146. &pSvc
  147. );
  148. SysFreeString(Ns);
  149. if (hRes)
  150. {
  151. printf("Could not connect. Error code = 0x%X\n", hRes);
  152. CoUninitialize();
  153. return FALSE;
  154. }
  155. pLoc->Release(); // Don't need the locator any more.
  156. // Return pointer to user.
  157. // =======================
  158. *pRetSvc = pSvc;
  159. return TRUE;
  160. }
  161. //***************************************************************************
  162. //
  163. // EnumClasses
  164. //
  165. // Enumerates all the available classes, showing only perf-related classes
  166. // (filtering out WBEM-specific classes).
  167. //
  168. //***************************************************************************
  169. void MenuEnumClasses(IN IWbemServices *pSvc)
  170. {
  171. IEnumWbemClassObject *pEnum = 0;
  172. HRESULT hRes = pSvc->CreateClassEnum(
  173. NULL,
  174. WBEM_FLAG_DEEP,
  175. NULL,
  176. &pEnum
  177. );
  178. if (hRes != WBEM_NO_ERROR)
  179. {
  180. printf("Failed to create enumerator\n");
  181. return;
  182. }
  183. // If here, we can show all the available classes.
  184. // ===============================================
  185. ULONG uTotalClasses = 0;
  186. ULONG uTotalPerfClasses = 0;
  187. for (;;)
  188. {
  189. // Ask for the classes one at a time.
  190. // ==================================
  191. IWbemClassObject *pObj = 0;
  192. ULONG uReturned = 0;
  193. hRes = pEnum->Next(
  194. 0, // timeout
  195. 1, // Ask for one object
  196. &pObj,
  197. &uReturned
  198. );
  199. uTotalClasses += uReturned;
  200. if (uReturned == 0)
  201. break;
  202. // See whether the class is perf-related or not by
  203. // checking for the "perf" qualifier on the class def.
  204. // ===================================================
  205. IWbemQualifierSet *pQSet = 0;
  206. pObj->GetQualifierSet(&pQSet);
  207. BSTR TargetQualifier = SysAllocString(L"perf");
  208. HRESULT hRes = pQSet->Get(TargetQualifier, 0, 0, 0);
  209. SysFreeString(TargetQualifier);
  210. if (hRes == WBEM_E_NOT_FOUND)
  211. {
  212. // Must be a WBEM class and not a perf class, so skip it.
  213. // ======================================================
  214. pObj->Release();
  215. continue; // Back to top of the forever loop
  216. }
  217. uTotalPerfClasses++;
  218. // Get the class name.
  219. // ===================
  220. VARIANT v;
  221. VariantInit(&v);
  222. BSTR PropName = SysAllocString(L"__CLASS");
  223. pObj->Get(PropName, 0, &v, 0, 0);
  224. printf("Retrieved perf class %S\n", V_BSTR(&v));
  225. SysFreeString(PropName);
  226. // Done with this object.
  227. // ======================
  228. pObj->Release();
  229. }
  230. // Cleanup.
  231. // ========
  232. pEnum->Release();
  233. printf("A total of %u WBEM classes and %u Perf Classes were retrieved\n",
  234. uTotalClasses,
  235. uTotalPerfClasses
  236. );
  237. }
  238. //***************************************************************************
  239. //
  240. // MenuEnumInstances
  241. //
  242. // Enumerates available instances for a given class.
  243. //
  244. //***************************************************************************
  245. void MenuEnumInstances(IWbemServices *pSvc)
  246. {
  247. wchar_t ClassName[128];
  248. printf("Enter class name:\n");
  249. _getws(ClassName);
  250. BSTR bstrClassName = SysAllocString(ClassName);
  251. IEnumWbemClassObject *pEnum = 0;
  252. HRESULT hRes = pSvc->CreateInstanceEnum(
  253. bstrClassName,
  254. WBEM_FLAG_DEEP,
  255. NULL,
  256. &pEnum
  257. );
  258. SysFreeString(bstrClassName);
  259. if (hRes != WBEM_NO_ERROR)
  260. {
  261. printf("Failed to create enumerator. Error code = 0x%X\n", hRes);
  262. return;
  263. }
  264. // If here, we can show all the available instances.
  265. // =================================================
  266. ULONG uTotalInstances = 0;
  267. for (;;)
  268. {
  269. // Ask for the classes one at a time.
  270. // ==================================
  271. IWbemClassObject *pObj = 0;
  272. ULONG uReturned = 0;
  273. hRes = pEnum->Next(
  274. 0, // timeout
  275. 1, // Ask for one object
  276. &pObj,
  277. &uReturned
  278. );
  279. uTotalInstances += uReturned;
  280. if (uReturned == 0)
  281. break;
  282. // Get the path so that we can identify the object.
  283. // ================================================
  284. VARIANT v;
  285. VariantInit(&v);
  286. BSTR Path = SysAllocString(L"__RELPATH");
  287. pObj->Get(Path, 0, &v, 0, 0);
  288. printf("Object = %S\n", V_BSTR(&v));
  289. VariantClear(&v);
  290. SysFreeString(Path);
  291. // Done with this object.
  292. // ======================
  293. pObj->Release();
  294. }
  295. // Cleanup.
  296. // ========
  297. pEnum->Release();
  298. printf("A total of %u instances were retrieved\n", uTotalInstances);
  299. }
  300. //***************************************************************************
  301. //
  302. // MenuCreateRefresher
  303. //
  304. // Creates the refresher for this sample.
  305. //
  306. //***************************************************************************
  307. void MenuCreateRefresher()
  308. {
  309. BOOL bRes = CreateRefresher();
  310. if (bRes == TRUE)
  311. printf("New refresher created.\n");
  312. else
  313. printf("Refresher failed to create\n");
  314. }
  315. //***************************************************************************
  316. //
  317. // MenuDeleteRefresher
  318. //
  319. // Deletes the refresher for this sample.
  320. //
  321. //***************************************************************************
  322. void MenuDeleteRefresher()
  323. {
  324. BOOL bRes = DestroyRefresher();
  325. if (bRes)
  326. printf("Success\n");
  327. else
  328. printf("Failed\n");
  329. }
  330. //***************************************************************************
  331. //
  332. // MenuAddObjectsToRefresher
  333. //
  334. // Adds arbitrary objects to the refresher.
  335. //
  336. //***************************************************************************
  337. void MenuAddObjectsToRefresher(IWbemServices *pSvc)
  338. {
  339. wchar_t ObjPath[256];
  340. printf("Enter object path:");
  341. _getws(ObjPath);
  342. BOOL bRes = AddObject(pSvc, ObjPath);
  343. if (bRes == FALSE)
  344. {
  345. printf("Failed to add object\n");
  346. return;
  347. }
  348. printf("Added object successfully\n");
  349. }
  350. //***************************************************************************
  351. //
  352. // MenuRemoveObjectsFromRefresher
  353. //
  354. // Removes a selected object from the refresher.
  355. //
  356. //***************************************************************************
  357. void MenuRemoveObjectsFromRefresher()
  358. {
  359. char buf[32];
  360. printf("Enter object id to remove:");
  361. LONG lObjId = atol(gets(buf));
  362. BOOL bRes = RemoveObject(lObjId);
  363. if (bRes)
  364. printf("Success\n");
  365. else
  366. printf("Failed to remove object\n");
  367. }
  368. //***************************************************************************
  369. //
  370. // MenuRefresh
  371. //
  372. // Executes a single refresh, which refreshes all the objects in the
  373. // refresher.
  374. //
  375. //***************************************************************************
  376. void MenuRefresh()
  377. {
  378. BOOL bRes = Refresh();
  379. if (bRes)
  380. printf("Sucess\n");
  381. else
  382. printf("Failed\n");
  383. }
  384. //***************************************************************************
  385. //
  386. // MenuShowObjectList
  387. //
  388. // Shows the paths of all the objects in the refresher.
  389. //
  390. //***************************************************************************
  391. void MenuShowObjectList()
  392. {
  393. ShowObjectList();
  394. }
  395. //***************************************************************************
  396. //
  397. // MenuDumpObject
  398. //
  399. // Given an ID, dumps that object to the screen.
  400. //
  401. //***************************************************************************
  402. void MenuDumpObject()
  403. {
  404. char buf[32];
  405. printf("Enter object id to dump:\n");
  406. LONG lObjId = atol(gets(buf));
  407. BOOL bRes = DumpObjectById(lObjId);
  408. if (bRes)
  409. printf("Success\n");
  410. else
  411. printf("Failed to locate and dump object\n");
  412. }
  413. //***************************************************************************
  414. //
  415. // TightRefreshLoop
  416. //
  417. // Executes a tight refresh loop with user-specified iterations and
  418. // Sleep time in between iterations. Used to check performance during
  419. // refresh execution.
  420. //
  421. //***************************************************************************
  422. void TightRefreshLoop()
  423. {
  424. char buf[32];
  425. LONG lSleep = 0;
  426. int nIterations = 0;
  427. printf("Enter total number of iterations:");
  428. nIterations = atoi(gets(buf));
  429. printf("Enter Sleep() time between iterations:");
  430. lSleep = atol(gets(buf));
  431. DWORD dwStart = GetCurrentTime();
  432. for (int i = 0; i < nIterations; i++)
  433. {
  434. Refresh();
  435. if (lSleep)
  436. Sleep(lSleep);
  437. }
  438. DWORD dwElapsed = GetCurrentTime() - dwStart;
  439. printf("Elapsed time = %d milliseconds\n", dwElapsed);
  440. }
  441. //***************************************************************************
  442. //
  443. // AddALotOfObjects
  444. //
  445. // Adds a bunch of copies of the same object to the refresher. This
  446. // helps to quickly simulate a load, since it is as difficult to refresh
  447. // 10 copies of the same object as to refresh 10 different objects.
  448. //
  449. //***************************************************************************
  450. void AddALotOfObjects(IWbemServices *pSvc)
  451. {
  452. printf("Adding many copies of the same object to the refresher to simulate a load.\n");
  453. wchar_t ObjPath[256];
  454. printf("Enter object path:");
  455. _getws(ObjPath);
  456. char buf[32];
  457. printf("Number of copies:");
  458. int nNumObjects = atoi(gets(buf));
  459. for (int i = 0; i < nNumObjects; i++)
  460. {
  461. BOOL bRes = AddObject(pSvc, ObjPath);
  462. if (bRes == FALSE)
  463. {
  464. printf("Failed to add object\n");
  465. return;
  466. }
  467. }
  468. printf("Added objects successfully\n");
  469. }