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.

574 lines
14 KiB

  1. /*++ BUILD Version: 0001 // Increment this if a change has global effects
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. perfras.c
  5. Abstract:
  6. This file implements the Extensible Objects for the Ras object type
  7. Created:
  8. Russ Blake 24 Feb 93
  9. Thomas J. Dimitri 28 May 93
  10. Revision History
  11. Ram Cherala 15 Feb 96
  12. Don't hard code the length of the instance name in
  13. CollectRasPerformanceData.
  14. PerfMon checks the actual instance name length to determine
  15. if the name is properly formatted, so compute it for each
  16. instance name.
  17. Patrick Y. Ng 12 Aug 93
  18. --*/
  19. //
  20. // Include Files
  21. //
  22. #include <nt.h>
  23. #include <ntrtl.h>
  24. #include <nturtl.h>
  25. #include <ntddser.h>
  26. #include <windows.h>
  27. #include <string.h>
  28. #include <wcstr.h>
  29. #include <winperf.h>
  30. #include <malloc.h>
  31. #include <ntprfctr.h>
  32. #include "globals.h"
  33. #include "rasctrs.h" // error message definition
  34. #include "perfmsg.h"
  35. #include "perfutil.h"
  36. #include "dataras.h"
  37. #include "port.h"
  38. #include <rasman.h>
  39. #include <serial.h>
  40. #include <isdn.h>
  41. #include <raserror.h>
  42. #include <stdarg.h>
  43. #include <string.h>
  44. #include <stdio.h>
  45. //
  46. // References to constants which initialize the Object type definitions
  47. //
  48. DWORD dwOpenCount = 0; // count of "Open" threads
  49. BOOL bInitOK = FALSE; // true = DLL initialized OK
  50. //
  51. // Function Prototypes
  52. //
  53. // these are used to insure that the data collection functions
  54. // accessed by Perflib will have the correct calling format.
  55. //
  56. PM_OPEN_PROC OpenRasPerformanceData;
  57. PM_COLLECT_PROC CollectRasPerformanceData;
  58. PM_CLOSE_PROC CloseRasPerformanceData;
  59. //***
  60. //
  61. // Routine Description:
  62. //
  63. // This routine will open and map the memory used by the RAS driver to
  64. // pass performance data in. This routine also initializes the data
  65. // structures used to pass data back to the registry
  66. //
  67. // Arguments:
  68. //
  69. // Pointer to object ID of each device to be opened (RAS)
  70. //
  71. //
  72. // Return Value:
  73. //
  74. // None.
  75. //
  76. //***
  77. DWORD OpenRasPerformanceData( LPWSTR lpDeviceNames )
  78. {
  79. LONG status;
  80. HKEY hKeyDriverPerf;
  81. DWORD size;
  82. DWORD type;
  83. //
  84. // Since SCREG is multi-threaded and will call this routine in
  85. // order to service remote performance queries, this library
  86. // must keep track of how many times it has been opened (i.e.
  87. // how many threads have accessed it). the registry routines will
  88. // limit access to the initialization routine to only one thread
  89. // at a time so synchronization (i.e. reentrancy) should not be
  90. // a problem
  91. //
  92. if (!dwOpenCount)
  93. {
  94. //
  95. // open Eventlog interface
  96. //
  97. hEventLog = MonOpenEventLog();
  98. //
  99. // Load rasman.dll and get all the required functions.
  100. //
  101. status = InitRasFunctions();
  102. if( status != ERROR_SUCCESS )
  103. {
  104. REPORT_ERROR (RASPERF_UNABLE_DO_IOCTL, LOG_USER);
  105. // this is fatal, if we can't get data then there's no
  106. // point in continuing.
  107. goto OpenExitPoint;
  108. }
  109. InitObjectCounterIndex( RAS_FIRST_COUNTER_INDEX,
  110. RAS_FIRST_HELP_INDEX );
  111. //
  112. // Initialize all the port information.
  113. //
  114. status = InitPortInfo();
  115. if (status != ERROR_SUCCESS)
  116. {
  117. REPORT_ERROR_DATA (RASPERF_UNABLE_CREATE_PORT_TABLE, LOG_USER,
  118. &status, sizeof(status));
  119. // this is fatal, if we can't get the base values of the
  120. // counter or help names, then the names won't be available
  121. // to the requesting application so there's not much
  122. // point in continuing.
  123. goto OpenExitPoint;
  124. }
  125. bInitOK = TRUE; // ok to use this function
  126. }
  127. dwOpenCount++; // increment OPEN counter
  128. status = ERROR_SUCCESS; // for successful exit
  129. OpenExitPoint:
  130. return status;
  131. }
  132. //***
  133. //
  134. // Routine Description:
  135. //
  136. // This routine will return the data for the RAS counters.
  137. //
  138. // Arguments:
  139. //
  140. // IN OUT LPWSTR lpValueName
  141. // pointer to a wide character string passed by registry.
  142. //
  143. // IN OUT LPVOID *lppData
  144. // IN: pointer to the address of the buffer to receive the completed
  145. // PerfDataBlock and subordinate structures. This routine will
  146. // append its data to the buffer starting at the point referenced
  147. // by *lppData.
  148. // OUT: points to the first byte after the data structure added by this
  149. // routine. This routine updated the value at lppdata after appending
  150. // its data.
  151. //
  152. // IN OUT LPDWORD lpcbTotalBytes
  153. // IN: the address of the DWORD that tells the size in bytes of the
  154. // buffer referenced by the lppData argument
  155. // OUT: the number of bytes added by this routine is written to the
  156. // DWORD pointed to by this argument
  157. //
  158. // IN OUT LPDWORD NumObjectTypes
  159. // IN: the address of the DWORD to receive the number of objects added
  160. // by this routine
  161. // OUT: the number of objects added by this routine is written to the
  162. // DWORD pointed to by this argument
  163. //
  164. // Return Value:
  165. //
  166. // ERROR_MORE_DATA if buffer passed is too small to hold data
  167. // any error conditions encountered are reported to the event log if
  168. // event logging is enabled.
  169. //
  170. // ERROR_SUCCESS if success or any other error. Errors, however are
  171. // also reported to the event log.
  172. //
  173. //***
  174. DWORD CollectRasPerformanceData(
  175. IN LPWSTR lpValueName,
  176. IN OUT LPVOID *lppData,
  177. IN OUT LPDWORD lpcbTotalBytes,
  178. IN OUT LPDWORD lpNumObjectTypes )
  179. {
  180. // Variables for reformating the data
  181. NTSTATUS Status;
  182. ULONG SpaceNeeded;
  183. PBYTE pbIn = (PBYTE) *lppData;
  184. // variables used for error logging
  185. DWORD dwQueryType;
  186. // Variables used to record which objects are required
  187. static BOOL IsRasPortObject;
  188. static BOOL IsRasTotalObject;
  189. //
  190. // Reset some output variables.
  191. //
  192. *lpNumObjectTypes = 0;
  193. //
  194. // before doing anything else, see if Open went OK
  195. //
  196. if (!bInitOK)
  197. {
  198. // unable to continue because open failed.
  199. *lpcbTotalBytes = (DWORD) 0;
  200. *lpNumObjectTypes = (DWORD) 0;
  201. return ERROR_SUCCESS; // yes, this is a successful exit
  202. }
  203. //
  204. // see if this is a foreign (i.e. non-NT) computer data request
  205. //
  206. dwQueryType = GetQueryType (lpValueName);
  207. if (dwQueryType == QUERY_FOREIGN)
  208. {
  209. // this routine does not service requests for data from
  210. // Non-NT computers
  211. *lpcbTotalBytes = (DWORD) 0;
  212. *lpNumObjectTypes = (DWORD) 0;
  213. return ERROR_SUCCESS;
  214. }
  215. else if (dwQueryType == QUERY_ITEMS)
  216. {
  217. IsRasPortObject = IsNumberInUnicodeList (gRasPortDataDefinition.RasObjectType.ObjectNameTitleIndex,
  218. lpValueName);
  219. IsRasTotalObject = IsNumberInUnicodeList (gRasTotalDataDefinition.RasObjectType.ObjectNameTitleIndex,
  220. lpValueName);
  221. if ( !IsRasPortObject && !IsRasTotalObject )
  222. {
  223. //
  224. // request received for data object not provided by this routine
  225. //
  226. *lpcbTotalBytes = (DWORD) 0;
  227. *lpNumObjectTypes = (DWORD) 0;
  228. return ERROR_SUCCESS;
  229. }
  230. }
  231. else if( dwQueryType == QUERY_GLOBAL )
  232. {
  233. IsRasPortObject = IsRasTotalObject = TRUE;
  234. }
  235. //
  236. // Now check to see if we have enough space to hold all the data
  237. //
  238. SpaceNeeded = GetSpaceNeeded(IsRasPortObject, IsRasTotalObject);
  239. // DbgPrint("RASCTRS: IN TotalBytes=0x%x, SpaceNeeded=0x%x\n",
  240. // *lpcbTotalBytes, SpaceNeeded);
  241. if ( *lpcbTotalBytes < SpaceNeeded )
  242. {
  243. *lpcbTotalBytes = (DWORD) 0;
  244. *lpNumObjectTypes = (DWORD) 0;
  245. return ERROR_MORE_DATA;
  246. }
  247. //
  248. // Collect all the RAS statistics now.
  249. //
  250. Status = CollectRasStatistics();
  251. if( Status != ERROR_SUCCESS )
  252. {
  253. REPORT_ERROR_DATA (RASPERF_CANNOT_GET_RAS_STATISTICS, LOG_USER,
  254. &Status, sizeof(Status));
  255. *lpcbTotalBytes = (DWORD) 0;
  256. *lpNumObjectTypes = (DWORD) 0;
  257. return ERROR_SUCCESS;
  258. }
  259. //DbgPrint("RASCTRS: pbIn = 0x%x\n", pbIn);
  260. //
  261. // We first fill in the data for object Ras Port, if needed.
  262. //
  263. if( IsRasPortObject )
  264. {
  265. PRAS_PORT_DATA_DEFINITION pRasPortDataDefinition;
  266. RAS_PORT_INSTANCE_DEFINITION RasPortInstanceDefinition;
  267. PRAS_PORT_INSTANCE_DEFINITION pRasPortInstanceDefinition;
  268. DWORD cPorts;
  269. DWORD i;
  270. PVOID pData;
  271. cPorts = GetNumOfPorts();
  272. pRasPortDataDefinition = (PRAS_PORT_DATA_DEFINITION) *lppData;
  273. //DbgPrint("RASCTRS: pRasPortDataDefinition = 0x%x\n", pRasPortDataDefinition);
  274. //
  275. // Copy the (constant, initialized) Object Type and counter definitions
  276. // to the caller's data buffer
  277. //
  278. memcpy( pRasPortDataDefinition,
  279. &gRasPortDataDefinition,
  280. sizeof(RAS_PORT_DATA_DEFINITION));
  281. //
  282. // Now copy the instance definition and counter block.
  283. //
  284. //
  285. // First construct the default perf instance definition.
  286. //
  287. RasPortInstanceDefinition.RasInstanceType.ByteLength =
  288. ALIGN8(sizeof(RAS_PORT_INSTANCE_DEFINITION));
  289. RasPortInstanceDefinition.RasInstanceType.ParentObjectTitleIndex = 0;
  290. RasPortInstanceDefinition.RasInstanceType.ParentObjectInstance = 0;
  291. RasPortInstanceDefinition.RasInstanceType.NameOffset =
  292. sizeof(PERF_INSTANCE_DEFINITION);
  293. //DbgPrint("RASCTRS: RasPortinstanceDefinition.ByteLength = 0x%x\n",
  294. // RasPortInstanceDefinition.RasInstanceType.ByteLength);
  295. /* Don't hard code the length of the instance name.
  296. ** PerfMon checks the actual instance name length to determine
  297. ** if the name is properly formatted, so compute it for
  298. ** each instance name. ramc 2/15/96.
  299. ** RasPortInstanceDefinition.RasInstanceType.NameLength =
  300. ** sizeof( WCHAR ) * MAX_PORT_NAME;
  301. */
  302. //
  303. // Get to the end of the data definition.
  304. //
  305. // pData = (PVOID) &(pRasPortDataDefinition[1]);
  306. pData = ((PBYTE) pRasPortDataDefinition + ALIGN8(sizeof(RAS_PORT_DATA_DEFINITION)));
  307. for( i=0; i < cPorts; i++ )
  308. {
  309. //DbgPrint("RASCTRS: port %d, pData = 0x%x\n", i, pData);
  310. //
  311. // First copy the instance definition data.
  312. //
  313. RasPortInstanceDefinition.RasInstanceType.UniqueID = PERF_NO_UNIQUE_ID;
  314. lstrcpyW( (LPWSTR)&RasPortInstanceDefinition.InstanceName,
  315. GetInstanceName(i) );
  316. // Compute the instance name length
  317. RasPortInstanceDefinition.RasInstanceType.NameLength =
  318. (lstrlenW(RasPortInstanceDefinition.InstanceName) + 1) *
  319. sizeof( WCHAR );
  320. memcpy( pData, &RasPortInstanceDefinition,
  321. sizeof( RasPortInstanceDefinition ) );
  322. //
  323. // Move pPerfInstanceDefinition to the beginning of data block.
  324. //
  325. pData = (PVOID)((PBYTE) pData + ALIGN8(sizeof(RAS_PORT_INSTANCE_DEFINITION)));
  326. //
  327. // Get the data block. Note that pPerfInstanceDefinition will be
  328. // set to the next available byte.
  329. //
  330. GetInstanceData( i, &pData );
  331. }
  332. //
  333. // Set *lppData to the next available byte.
  334. //
  335. *lppData = pData;
  336. (*lpNumObjectTypes)++;
  337. }
  338. //
  339. // Then we fill in the data for object Ras Total, if needed.
  340. //
  341. if( IsRasTotalObject )
  342. {
  343. PRAS_TOTAL_DATA_DEFINITION pRasTotalDataDefinition;
  344. PVOID pData;
  345. pRasTotalDataDefinition = (PRAS_TOTAL_DATA_DEFINITION) *lppData;
  346. //DbgPrint("RASCTRS: RasTotalDataDefinition = 0x%x\n",
  347. // pRasTotalDataDefinition);
  348. //
  349. // Copy the (constant, initialized) Object Type and counter definitions
  350. // to the caller's data buffer
  351. //
  352. memcpy( pRasTotalDataDefinition,
  353. &gRasTotalDataDefinition,
  354. sizeof(RAS_TOTAL_DATA_DEFINITION));
  355. //
  356. // Now copy the counter block.
  357. //
  358. //
  359. // Set pRasTotalDataDefinition to the beginning of counter block.
  360. //
  361. // pData = (PVOID) &(pRasTotalDataDefinition[1]);
  362. pData = (PBYTE) pRasTotalDataDefinition + ALIGN8(sizeof(RAS_TOTAL_DATA_DEFINITION));
  363. //DbgPrint("RASCTRS: pData for total = 0x%x\n", pData);
  364. GetTotalData( &pData );
  365. //
  366. // Set *lppData to the next available byte.
  367. //
  368. *lppData = pData;
  369. (*lpNumObjectTypes)++;
  370. }
  371. //DbgPrint("RASCTRS: pbOut = 0x%x\n", *lppData);
  372. *lpcbTotalBytes = SpaceNeeded;
  373. /*
  374. DbgPrint("pbIn+SpaceNeeded=0x%x, *lppData=0x%x\n",
  375. pbIn+SpaceNeeded,
  376. *lppData);
  377. */
  378. ASSERT((pbIn + SpaceNeeded) == (PBYTE) *lppData);
  379. //DbgPrint("RASCTRS: OUT TotalBytes=0x%x\n", *lpcbTotalBytes);
  380. return ERROR_SUCCESS;
  381. }
  382. //***
  383. //
  384. // Routine Description:
  385. //
  386. // This routine closes the open handles to RAS device performance
  387. // counters.
  388. //
  389. // Arguments:
  390. //
  391. // None.
  392. //
  393. // Return Value:
  394. //
  395. // ERROR_SUCCESS
  396. //
  397. //***
  398. DWORD CloseRasPerformanceData()
  399. {
  400. if (!(--dwOpenCount))
  401. {
  402. // when this is the last thread...
  403. MonCloseEventLog();
  404. ClosePortInfo();
  405. }
  406. return ERROR_SUCCESS;
  407. }