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.

690 lines
20 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. faxperf.cpp
  5. Abstract:
  6. This module contains the fax perfom dll code.
  7. Author:
  8. Wesley Witt (wesw) 22-Aug-1996
  9. --*/
  10. #include <windows.h>
  11. #include <winperf.h>
  12. #include "faxcount.h"
  13. #include "faxperf.h"
  14. #include "faxreg.h"
  15. #define FAX_NUM_PERF_OBJECT_TYPES 1
  16. #define COUNTER_SIZE sizeof(DWORD)
  17. #define INBOUND_BYTES_OFFSET (COUNTER_SIZE ) // 1
  18. #define INBOUND_FAXES_OFFSET (INBOUND_BYTES_OFFSET + COUNTER_SIZE) // 2
  19. #define INBOUND_PAGES_OFFSET (INBOUND_FAXES_OFFSET + COUNTER_SIZE) // 3
  20. #define INBOUND_MINUTES_OFFSET (INBOUND_PAGES_OFFSET + COUNTER_SIZE) // 4
  21. #define INBOUND_FAILED_RECEIVE_OFFSET (INBOUND_MINUTES_OFFSET + COUNTER_SIZE) // 5
  22. #define OUTBOUND_BYTES_OFFSET (INBOUND_FAILED_RECEIVE_OFFSET + COUNTER_SIZE) // 6
  23. #define OUTBOUND_FAXES_OFFSET (OUTBOUND_BYTES_OFFSET + COUNTER_SIZE) // 7
  24. #define OUTBOUND_PAGES_OFFSET (OUTBOUND_FAXES_OFFSET + COUNTER_SIZE) // 8
  25. #define OUTBOUND_MINUTES_OFFSET (OUTBOUND_PAGES_OFFSET + COUNTER_SIZE) // 9
  26. #define OUTBOUND_FAILED_CONNECTIONS_OFFSET (OUTBOUND_MINUTES_OFFSET + COUNTER_SIZE) // 10
  27. #define OUTBOUND_FAILED_XMIT_OFFSET (OUTBOUND_FAILED_CONNECTIONS_OFFSET + COUNTER_SIZE) // 11
  28. #define TOTAL_BYTES_OFFSET (OUTBOUND_FAILED_XMIT_OFFSET + COUNTER_SIZE) // 12
  29. #define TOTAL_FAXES_OFFSET (TOTAL_BYTES_OFFSET + COUNTER_SIZE) // 13
  30. #define TOTAL_PAGES_OFFSET (TOTAL_FAXES_OFFSET + COUNTER_SIZE) // 14
  31. #define TOTAL_MINUTES_OFFSET (TOTAL_PAGES_OFFSET + COUNTER_SIZE) // 15
  32. #define LAST_COUNTER_OFFSET (TOTAL_MINUTES_OFFSET + COUNTER_SIZE) //
  33. #define SIZE_OF_FAX_PERFORMANCE_DATA LAST_COUNTER_OFFSET
  34. #define PERF_COUNTER_DEFINITION(nm,ty) \
  35. { \
  36. sizeof(PERF_COUNTER_DEFINITION), \
  37. nm, \
  38. 0, \
  39. nm, \
  40. 0, \
  41. 0, \
  42. PERF_DETAIL_NOVICE, \
  43. ty, \
  44. COUNTER_SIZE, \
  45. nm##_OFFSET \
  46. }
  47. #define PERF_COUNTER_INC(nm) \
  48. FaxDataDefinition.nm##.CounterNameTitleIndex += dwFirstCounter; \
  49. FaxDataDefinition.nm##.CounterHelpTitleIndex += dwFirstHelp
  50. #pragma pack (4)
  51. typedef struct _FAX_DATA_DEFINITION {
  52. PERF_OBJECT_TYPE FaxObjectType;
  53. PERF_COUNTER_DEFINITION InboundBytes;
  54. PERF_COUNTER_DEFINITION InboundFaxes;
  55. PERF_COUNTER_DEFINITION InboundPages;
  56. PERF_COUNTER_DEFINITION InboundMinutes;
  57. PERF_COUNTER_DEFINITION InboundFailedReceive;
  58. PERF_COUNTER_DEFINITION OutboundBytes;
  59. PERF_COUNTER_DEFINITION OutboundFaxes;
  60. PERF_COUNTER_DEFINITION OutboundPages;
  61. PERF_COUNTER_DEFINITION OutboundMinutes;
  62. PERF_COUNTER_DEFINITION OutboundFailedConnections;
  63. PERF_COUNTER_DEFINITION OutboundFailedXmit;
  64. PERF_COUNTER_DEFINITION TotalBytes;
  65. PERF_COUNTER_DEFINITION TotalFaxes;
  66. PERF_COUNTER_DEFINITION TotalPages;
  67. PERF_COUNTER_DEFINITION TotalMinutes;
  68. } FAX_DATA_DEFINITION, *PFAX_DATA_DEFINITION;
  69. #pragma pack ()
  70. FAX_DATA_DEFINITION FaxDataDefinition = {
  71. {
  72. sizeof(FAX_DATA_DEFINITION) + SIZE_OF_FAX_PERFORMANCE_DATA,
  73. sizeof(FAX_DATA_DEFINITION),
  74. sizeof(PERF_OBJECT_TYPE),
  75. FAXOBJ,
  76. 0,
  77. FAXOBJ,
  78. 0,
  79. PERF_DETAIL_NOVICE,
  80. (sizeof(FAX_DATA_DEFINITION)-sizeof(PERF_OBJECT_TYPE))/sizeof(PERF_COUNTER_DEFINITION),
  81. 0,
  82. PERF_NO_INSTANCES,
  83. 0,
  84. },
  85. PERF_COUNTER_DEFINITION( INBOUND_BYTES, PERF_COUNTER_RAWCOUNT ),
  86. PERF_COUNTER_DEFINITION( INBOUND_FAXES, PERF_COUNTER_RAWCOUNT ),
  87. PERF_COUNTER_DEFINITION( INBOUND_PAGES, PERF_COUNTER_RAWCOUNT ),
  88. PERF_COUNTER_DEFINITION( INBOUND_MINUTES, PERF_COUNTER_RAWCOUNT ),
  89. PERF_COUNTER_DEFINITION( INBOUND_FAILED_RECEIVE, PERF_COUNTER_RAWCOUNT ),
  90. PERF_COUNTER_DEFINITION( OUTBOUND_BYTES, PERF_COUNTER_RAWCOUNT ),
  91. PERF_COUNTER_DEFINITION( OUTBOUND_FAXES, PERF_COUNTER_RAWCOUNT ),
  92. PERF_COUNTER_DEFINITION( OUTBOUND_PAGES, PERF_COUNTER_RAWCOUNT ),
  93. PERF_COUNTER_DEFINITION( OUTBOUND_MINUTES, PERF_COUNTER_RAWCOUNT ),
  94. PERF_COUNTER_DEFINITION( OUTBOUND_FAILED_CONNECTIONS, PERF_COUNTER_RAWCOUNT ),
  95. PERF_COUNTER_DEFINITION( OUTBOUND_FAILED_XMIT, PERF_COUNTER_RAWCOUNT ),
  96. PERF_COUNTER_DEFINITION( TOTAL_BYTES, PERF_COUNTER_RAWCOUNT ),
  97. PERF_COUNTER_DEFINITION( TOTAL_FAXES, PERF_COUNTER_RAWCOUNT ),
  98. PERF_COUNTER_DEFINITION( TOTAL_PAGES, PERF_COUNTER_RAWCOUNT ),
  99. PERF_COUNTER_DEFINITION( TOTAL_MINUTES, PERF_COUNTER_RAWCOUNT )
  100. };
  101. #define QUERY_GLOBAL 1
  102. #define QUERY_ITEMS 2
  103. #define QUERY_FOREIGN 3
  104. #define QUERY_COSTLY 4
  105. WCHAR GLOBAL_STRING[] = L"Global";
  106. WCHAR FOREIGN_STRING[] = L"Foreign";
  107. WCHAR COSTLY_STRING[] = L"Costly";
  108. WCHAR NULL_STRING[] = L"\0";
  109. // test for delimiter, end of line and non-digit characters
  110. // used by IsNumberInUnicodeList routine
  111. //
  112. #define DIGIT 1
  113. #define DELIMITER 2
  114. #define INVALID 3
  115. #define EvalThisChar(c,d) ( \
  116. (c == d) ? DELIMITER : \
  117. (c == 0) ? DELIMITER : \
  118. (c < (WCHAR)'0') ? INVALID : \
  119. (c > (WCHAR)'9') ? INVALID : \
  120. DIGIT)
  121. DWORD dwOpenCount = 0;
  122. BOOL bInitOK = FALSE;
  123. HANDLE hMap;
  124. PFAX_PERF_COUNTERS PerfCounters;
  125. DWORD
  126. GetQueryType (
  127. IN LPWSTR lpValue
  128. )
  129. /*++
  130. GetQueryType
  131. returns the type of query described in the lpValue string so that
  132. the appropriate processing method may be used
  133. Arguments
  134. IN lpValue
  135. string passed to PerfRegQuery Value for processing
  136. Return Value
  137. QUERY_GLOBAL
  138. if lpValue == 0 (null pointer)
  139. lpValue == pointer to Null string
  140. lpValue == pointer to "Global" string
  141. QUERY_FOREIGN
  142. if lpValue == pointer to "Foriegn" string
  143. QUERY_COSTLY
  144. if lpValue == pointer to "Costly" string
  145. otherwise:
  146. QUERY_ITEMS
  147. --*/
  148. {
  149. WCHAR *pwcArgChar, *pwcTypeChar;
  150. BOOL bFound;
  151. if (lpValue == 0) {
  152. return QUERY_GLOBAL;
  153. } else if (*lpValue == 0) {
  154. return QUERY_GLOBAL;
  155. }
  156. // check for "Global" request
  157. pwcArgChar = lpValue;
  158. pwcTypeChar = GLOBAL_STRING;
  159. bFound = TRUE; // assume found until contradicted
  160. // check to the length of the shortest string
  161. while ((*pwcArgChar != 0) && (*pwcTypeChar != 0)) {
  162. if (*pwcArgChar++ != *pwcTypeChar++) {
  163. bFound = FALSE; // no match
  164. break; // bail out now
  165. }
  166. }
  167. if (bFound) return QUERY_GLOBAL;
  168. // check for "Foreign" request
  169. pwcArgChar = lpValue;
  170. pwcTypeChar = FOREIGN_STRING;
  171. bFound = TRUE; // assume found until contradicted
  172. // check to the length of the shortest string
  173. while ((*pwcArgChar != 0) && (*pwcTypeChar != 0)) {
  174. if (*pwcArgChar++ != *pwcTypeChar++) {
  175. bFound = FALSE; // no match
  176. break; // bail out now
  177. }
  178. }
  179. if (bFound) return QUERY_FOREIGN;
  180. // check for "Costly" request
  181. pwcArgChar = lpValue;
  182. pwcTypeChar = COSTLY_STRING;
  183. bFound = TRUE; // assume found until contradicted
  184. // check to the length of the shortest string
  185. while ((*pwcArgChar != 0) && (*pwcTypeChar != 0)) {
  186. if (*pwcArgChar++ != *pwcTypeChar++) {
  187. bFound = FALSE; // no match
  188. break; // bail out now
  189. }
  190. }
  191. if (bFound) return QUERY_COSTLY;
  192. // if not Global and not Foreign and not Costly,
  193. // then it must be an item list
  194. return QUERY_ITEMS;
  195. }
  196. BOOL
  197. IsNumberInUnicodeList (
  198. IN DWORD dwNumber,
  199. IN LPWSTR lpwszUnicodeList
  200. )
  201. /*++
  202. IsNumberInUnicodeList
  203. Arguments:
  204. IN dwNumber
  205. DWORD number to find in list
  206. IN lpwszUnicodeList
  207. Null terminated, Space delimited list of decimal numbers
  208. Return Value:
  209. TRUE:
  210. dwNumber was found in the list of unicode number strings
  211. FALSE:
  212. dwNumber was not found in the list.
  213. --*/
  214. {
  215. DWORD dwThisNumber;
  216. WCHAR *pwcThisChar;
  217. BOOL bValidNumber;
  218. BOOL bNewItem;
  219. WCHAR wcDelimiter; // could be an argument to be more flexible
  220. if (lpwszUnicodeList == 0) return FALSE; // null pointer, # not founde
  221. pwcThisChar = lpwszUnicodeList;
  222. dwThisNumber = 0;
  223. wcDelimiter = (WCHAR)' ';
  224. bValidNumber = FALSE;
  225. bNewItem = TRUE;
  226. while (TRUE) {
  227. switch (EvalThisChar (*pwcThisChar, wcDelimiter)) {
  228. case DIGIT:
  229. // if this is the first digit after a delimiter, then
  230. // set flags to start computing the new number
  231. if (bNewItem) {
  232. bNewItem = FALSE;
  233. bValidNumber = TRUE;
  234. }
  235. if (bValidNumber) {
  236. dwThisNumber *= 10;
  237. dwThisNumber += (*pwcThisChar - (WCHAR)'0');
  238. }
  239. break;
  240. case DELIMITER:
  241. // a delimter is either the delimiter character or the
  242. // end of the string ('\0') if when the delimiter has been
  243. // reached a valid number was found, then compare it to the
  244. // number from the argument list. if this is the end of the
  245. // string and no match was found, then return.
  246. //
  247. if (bValidNumber) {
  248. if (dwThisNumber == dwNumber) return TRUE;
  249. bValidNumber = FALSE;
  250. }
  251. if (*pwcThisChar == 0) {
  252. return FALSE;
  253. } else {
  254. bNewItem = TRUE;
  255. dwThisNumber = 0;
  256. }
  257. break;
  258. case INVALID:
  259. // if an invalid character was encountered, ignore all
  260. // characters up to the next delimiter and then start fresh.
  261. // the invalid number is not compared.
  262. bValidNumber = FALSE;
  263. break;
  264. default:
  265. break;
  266. }
  267. pwcThisChar++;
  268. }
  269. } // IsNumberInUnicodeList
  270. DWORD APIENTRY
  271. OpenFaxPerformanceData(
  272. LPWSTR lpDeviceNames
  273. )
  274. /*++
  275. Routine Description:
  276. This routine will open and map the memory used by the Fax Service to
  277. pass performance data in. This routine also initializes the data
  278. structures used to pass data back to the registry
  279. Arguments:
  280. Pointer to object ID of each device to be opened
  281. Return Value:
  282. None.
  283. --*/
  284. {
  285. LONG status;
  286. HKEY hKeyDriverPerf = NULL;
  287. DWORD size;
  288. DWORD type;
  289. DWORD dwFirstCounter;
  290. DWORD dwFirstHelp;
  291. //
  292. // Since SCREG is multi-threaded and will call this routine in
  293. // order to service remote performance queries, this library
  294. // must keep track of how many times it has been opened (i.e.
  295. // how many threads have accessed it). the registry routines will
  296. // limit access to the initialization routine to only one thread
  297. // at a time so synchronization (i.e. reentrancy) should not be
  298. // a problem
  299. //
  300. if (!dwOpenCount) {
  301. hMap = OpenFileMapping(
  302. FILE_MAP_READ,
  303. FALSE,
  304. FAXPERF_SHARED_MEMORY
  305. );
  306. if (!hMap) {
  307. goto OpenExitPoint;
  308. }
  309. PerfCounters = (PFAX_PERF_COUNTERS) MapViewOfFile(
  310. hMap,
  311. FILE_MAP_READ,
  312. 0,
  313. 0,
  314. 0
  315. );
  316. if (!PerfCounters) {
  317. goto OpenExitPoint;
  318. }
  319. // get counter and help index base values from registry
  320. // Open key to registry entry
  321. // read First Counter and First Help values
  322. // update static data strucutures by adding base to
  323. // offset value in structure.
  324. status = RegOpenKeyEx(
  325. HKEY_LOCAL_MACHINE,
  326. REGKEY_FAXPERF,
  327. 0L,
  328. KEY_READ,
  329. &hKeyDriverPerf
  330. );
  331. if (status != ERROR_SUCCESS) {
  332. goto OpenExitPoint;
  333. }
  334. size = sizeof (DWORD);
  335. status = RegQueryValueEx(
  336. hKeyDriverPerf,
  337. "First Counter",
  338. 0L,
  339. &type,
  340. (LPBYTE)&dwFirstCounter,
  341. &size
  342. );
  343. if (status != ERROR_SUCCESS) {
  344. // this is fatal, if we can't get the base values of the
  345. // counter or help names, then the names won't be available
  346. // to the requesting application so there's not much
  347. // point in continuing.
  348. goto OpenExitPoint;
  349. }
  350. size = sizeof (DWORD);
  351. status = RegQueryValueEx(
  352. hKeyDriverPerf,
  353. "First Help",
  354. 0L,
  355. &type,
  356. (LPBYTE)&dwFirstHelp,
  357. &size
  358. );
  359. if (status != ERROR_SUCCESS) {
  360. // this is fatal, if we can't get the base values of the
  361. // counter or help names, then the names won't be available
  362. // to the requesting application so there's not much
  363. // point in continuing.
  364. goto OpenExitPoint;
  365. }
  366. FaxDataDefinition.FaxObjectType.ObjectNameTitleIndex += dwFirstCounter;
  367. FaxDataDefinition.FaxObjectType.ObjectHelpTitleIndex += dwFirstHelp;
  368. PERF_COUNTER_INC( InboundBytes );
  369. PERF_COUNTER_INC( InboundFaxes );
  370. PERF_COUNTER_INC( InboundPages );
  371. PERF_COUNTER_INC( InboundMinutes );
  372. PERF_COUNTER_INC( InboundFailedReceive );
  373. PERF_COUNTER_INC( OutboundBytes );
  374. PERF_COUNTER_INC( OutboundFaxes );
  375. PERF_COUNTER_INC( OutboundPages );
  376. PERF_COUNTER_INC( OutboundMinutes );
  377. PERF_COUNTER_INC( OutboundFailedConnections );
  378. PERF_COUNTER_INC( OutboundFailedXmit );
  379. PERF_COUNTER_INC( TotalBytes );
  380. PERF_COUNTER_INC( TotalFaxes );
  381. PERF_COUNTER_INC( TotalPages );
  382. PERF_COUNTER_INC( TotalMinutes );
  383. RegCloseKey (hKeyDriverPerf); // close key to registry
  384. bInitOK = TRUE; // ok to use this function
  385. }
  386. InterlockedIncrement( (PLONG)&dwOpenCount); // increment OPEN counter
  387. status = ERROR_SUCCESS; // for successful exit
  388. OpenExitPoint:
  389. if (!bInitOK) {
  390. if (hKeyDriverPerf) {
  391. RegCloseKey (hKeyDriverPerf);
  392. }
  393. if (PerfCounters) {
  394. UnmapViewOfFile(PerfCounters);
  395. }
  396. if (hMap) {
  397. CloseHandle( hMap );
  398. }
  399. }
  400. //
  401. // the performance APIs log an error in eventvwr if you fail this call
  402. // so we always return ERROR_SUCCESS so we don't clutter the logs
  403. //
  404. //return status;
  405. return ERROR_SUCCESS;
  406. }
  407. DWORD APIENTRY
  408. CollectFaxPerformanceData(
  409. IN LPWSTR lpValueName,
  410. IN OUT LPVOID *lppData,
  411. IN OUT LPDWORD lpcbTotalBytes,
  412. IN OUT LPDWORD lpNumObjectTypes
  413. )
  414. /*++
  415. Routine Description:
  416. This routine will return the data for the Fax counters.
  417. Arguments:
  418. IN LPWSTR lpValueName
  419. pointer to a wide character string passed by registry.
  420. IN OUT LPVOID *lppData
  421. IN: pointer to the address of the buffer to receive the completed
  422. PerfDataBlock and subordinate structures. This routine will
  423. append its data to the buffer starting at the point referenced
  424. by *lppData.
  425. OUT: points to the first byte after the data structure added by this
  426. routine. This routine updated the value at lppdata after appending
  427. its data.
  428. IN OUT LPDWORD lpcbTotalBytes
  429. IN: the address of the DWORD that tells the size in bytes of the
  430. buffer referenced by the lppData argument
  431. OUT: the number of bytes added by this routine is writted to the
  432. DWORD pointed to by this argument
  433. IN OUT LPDWORD NumObjectTypes
  434. IN: the address of the DWORD to receive the number of objects added
  435. by this routine
  436. OUT: the number of objects added by this routine is writted to the
  437. DWORD pointed to by this argument
  438. Return Value:
  439. ERROR_MORE_DATA if buffer passed is too small to hold data
  440. any error conditions encountered are reported to the event log if
  441. event logging is enabled.
  442. ERROR_SUCCESS if success or any other error. Errors, however are
  443. also reported to the event log.
  444. --*/
  445. {
  446. LPDWORD pData;
  447. ULONG SpaceNeeded;
  448. DWORD dwQueryType;
  449. //
  450. // before doing anything else, see if Open went OK
  451. //
  452. if (!bInitOK) {
  453. // unable to continue because open failed.
  454. *lpcbTotalBytes = 0;
  455. *lpNumObjectTypes = 0;
  456. return ERROR_SUCCESS;
  457. }
  458. // see if this is a foreign (i.e. non-NT) computer data request
  459. //
  460. dwQueryType = GetQueryType (lpValueName);
  461. if (dwQueryType == QUERY_FOREIGN) {
  462. // this routine does not service requests for data from
  463. // Non-NT computers
  464. *lpcbTotalBytes = 0;
  465. *lpNumObjectTypes = 0;
  466. return ERROR_SUCCESS;
  467. }
  468. if (dwQueryType == QUERY_ITEMS){
  469. if (!(IsNumberInUnicodeList (FaxDataDefinition.FaxObjectType.ObjectNameTitleIndex, lpValueName))) {
  470. // request received for data object not provided by this routine
  471. *lpcbTotalBytes = 0;
  472. *lpNumObjectTypes = 0;
  473. return ERROR_SUCCESS;
  474. }
  475. }
  476. SpaceNeeded = sizeof(FAX_DATA_DEFINITION) + SIZE_OF_FAX_PERFORMANCE_DATA;
  477. if ( *lpcbTotalBytes < SpaceNeeded ) {
  478. *lpcbTotalBytes = 0;
  479. *lpNumObjectTypes = 0;
  480. return ERROR_MORE_DATA;
  481. }
  482. pData = (LPDWORD) *lppData;
  483. //
  484. // Copy the (constant, initialized) Object Type and counter definitions
  485. // to the caller's data buffer
  486. //
  487. CopyMemory(
  488. pData,
  489. &FaxDataDefinition,
  490. sizeof(FAX_DATA_DEFINITION)
  491. );
  492. pData = (LPDWORD)((LPBYTE)pData + sizeof(FAX_DATA_DEFINITION));
  493. //
  494. // Format and collect Fax data from the service
  495. //
  496. *pData = SIZE_OF_FAX_PERFORMANCE_DATA;
  497. pData += 1;
  498. CopyMemory( pData, PerfCounters, sizeof(FAX_PERF_COUNTERS) );
  499. pData = (LPDWORD)((LPBYTE)pData + sizeof(FAX_PERF_COUNTERS));
  500. *lpNumObjectTypes = FAX_NUM_PERF_OBJECT_TYPES;
  501. *lpcbTotalBytes = (DWORD)((LPBYTE)pData - (LPBYTE)*lppData);
  502. *lppData = (PVOID) pData;
  503. return ERROR_SUCCESS;
  504. }
  505. DWORD APIENTRY
  506. CloseFaxPerformanceData(
  507. )
  508. /*++
  509. Routine Description:
  510. This routine closes the open handles to Fax performance counters
  511. Arguments:
  512. None.
  513. Return Value:
  514. ERROR_SUCCESS
  515. --*/
  516. {
  517. InterlockedDecrement( (PLONG)&dwOpenCount );
  518. if ((dwOpenCount == 0) && bInitOK) {
  519. if (PerfCounters) {
  520. UnmapViewOfFile(PerfCounters);
  521. PerfCounters = NULL;
  522. }
  523. if (hMap) {
  524. CloseHandle( hMap );
  525. hMap = NULL;
  526. }
  527. bInitOK = FALSE;
  528. }
  529. return ERROR_SUCCESS;
  530. }