PerformanceCounters.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Diagnostics
  5. {
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Reflection;
  11. using System.Runtime;
  12. using System.Runtime.CompilerServices;
  13. using System.Runtime.Diagnostics;
  14. using System.Security;
  15. using System.Security.Permissions;
  16. using System.ServiceModel;
  17. using System.ServiceModel.Channels;
  18. using System.ServiceModel.Configuration;
  19. using System.ServiceModel.Description;
  20. using System.ServiceModel.Dispatcher;
  21. static class PerformanceCounters
  22. {
  23. static PerformanceCounterScope scope;
  24. static object perfCounterDictionarySyncObject = new object();
  25. internal const int MaxInstanceNameLength = 127;
  26. static bool serviceOOM = false;
  27. static bool endpointOOM = false;
  28. static bool operationOOM = false;
  29. //we need a couple of ways of accessing the same performance counters. Normally, we know which endpoint
  30. //for which we need to update a perf counter. In some cases (e.g. RM), we only have a base uri. In those
  31. //cases, we update all the perf counters associated with the base uri. These two dictionaries point to
  32. //the same underlying perf counters, but in different ways.
  33. static Dictionary<string, ServiceModelPerformanceCounters> performanceCounters = null;
  34. static Dictionary<string, ServiceModelPerformanceCountersEntry> performanceCountersBaseUri = null;
  35. static List<ServiceModelPerformanceCounters> performanceCountersList = null;
  36. static internal PerformanceCounterScope Scope
  37. {
  38. get
  39. {
  40. return PerformanceCounters.scope;
  41. }
  42. set
  43. {
  44. PerformanceCounters.scope = value;
  45. }
  46. }
  47. static internal bool PerformanceCountersEnabled
  48. {
  49. get
  50. {
  51. return (PerformanceCounters.scope != PerformanceCounterScope.Off) &&
  52. (PerformanceCounters.scope != PerformanceCounterScope.Default);
  53. }
  54. }
  55. static internal bool MinimalPerformanceCountersEnabled
  56. {
  57. get
  58. {
  59. return (PerformanceCounters.scope == PerformanceCounterScope.Default);
  60. }
  61. }
  62. static PerformanceCounters()
  63. {
  64. PerformanceCounterScope scope = GetPerformanceCountersFromConfig();
  65. if (PerformanceCounterScope.Off != scope)
  66. {
  67. try
  68. {
  69. if (scope == PerformanceCounterScope.Default)
  70. {
  71. scope = OSEnvironmentHelper.IsVistaOrGreater ? PerformanceCounterScope.ServiceOnly : PerformanceCounterScope.Off;
  72. }
  73. PerformanceCounters.scope = scope;
  74. }
  75. catch (SecurityException securityException)
  76. {
  77. //switch off the counters - not supported in PT
  78. PerformanceCounters.scope = PerformanceCounterScope.Off;
  79. // not re-throwing on purpose
  80. DiagnosticUtility.TraceHandledException(securityException, TraceEventType.Warning);
  81. if (DiagnosticUtility.ShouldTraceWarning)
  82. {
  83. TraceUtility.TraceEvent(System.Diagnostics.TraceEventType.Warning,
  84. TraceCode.PerformanceCounterFailedToLoad,
  85. SR.GetString(SR.PartialTrustPerformanceCountersNotEnabled));
  86. }
  87. }
  88. }
  89. else
  90. {
  91. PerformanceCounters.scope = PerformanceCounterScope.Off;
  92. }
  93. }
  94. [Fx.Tag.SecurityNote(Critical = "Calls SecurityCritical method UnsafeGetSection which elevates in order to load config.",
  95. Safe = "Does not leak any config objects.")]
  96. [SecuritySafeCritical]
  97. static PerformanceCounterScope GetPerformanceCountersFromConfig()
  98. {
  99. return DiagnosticSection.UnsafeGetSection().PerformanceCounters;
  100. }
  101. static internal PerformanceCounter GetOperationPerformanceCounter(string perfCounterName, string instanceName)
  102. {
  103. return PerformanceCounters.GetPerformanceCounter(
  104. PerformanceCounterStrings.SERVICEMODELOPERATION.OperationPerfCounters,
  105. perfCounterName,
  106. instanceName,
  107. PerformanceCounterInstanceLifetime.Process);
  108. }
  109. static internal PerformanceCounter GetEndpointPerformanceCounter(string perfCounterName, string instanceName)
  110. {
  111. return PerformanceCounters.GetPerformanceCounter(
  112. PerformanceCounterStrings.SERVICEMODELENDPOINT.EndpointPerfCounters,
  113. perfCounterName,
  114. instanceName,
  115. PerformanceCounterInstanceLifetime.Process);
  116. }
  117. static internal PerformanceCounter GetServicePerformanceCounter(string perfCounterName, string instanceName)
  118. {
  119. return PerformanceCounters.GetPerformanceCounter(
  120. PerformanceCounterStrings.SERVICEMODELSERVICE.ServicePerfCounters,
  121. perfCounterName,
  122. instanceName,
  123. PerformanceCounterInstanceLifetime.Process);
  124. }
  125. static internal PerformanceCounter GetDefaultPerformanceCounter(string perfCounterName, string instanceName)
  126. {
  127. return PerformanceCounters.GetPerformanceCounter(
  128. PerformanceCounterStrings.SERVICEMODELSERVICE.ServicePerfCounters,
  129. perfCounterName,
  130. instanceName,
  131. PerformanceCounterInstanceLifetime.Global);
  132. }
  133. static internal PerformanceCounter GetPerformanceCounter(string categoryName, string perfCounterName, string instanceName, PerformanceCounterInstanceLifetime instanceLifetime)
  134. {
  135. PerformanceCounter counter = null;
  136. if (PerformanceCounters.PerformanceCountersEnabled || PerformanceCounters.MinimalPerformanceCountersEnabled)
  137. {
  138. counter = PerformanceCounters.GetPerformanceCounterInternal(categoryName, perfCounterName, instanceName, instanceLifetime);
  139. }
  140. return counter;
  141. }
  142. static internal PerformanceCounter GetPerformanceCounterInternal(string categoryName, string perfCounterName, string instanceName, PerformanceCounterInstanceLifetime instanceLifetime)
  143. {
  144. PerformanceCounter counter = null;
  145. try
  146. {
  147. counter = new PerformanceCounter();
  148. counter.CategoryName = categoryName;
  149. counter.CounterName = perfCounterName;
  150. counter.InstanceName = instanceName;
  151. counter.ReadOnly = false;
  152. counter.InstanceLifetime = instanceLifetime;
  153. // We now need to access the counter raw data to
  154. // force the counter object to be initialized. This
  155. // will force any exceptions due to mis-installation
  156. // of counters to occur here and be traced appropriately.
  157. try
  158. {
  159. long rawValue = counter.RawValue;
  160. }
  161. catch (InvalidOperationException)
  162. {
  163. counter = null;
  164. throw;
  165. }
  166. catch (SecurityException securityException)
  167. {
  168. // Cannot access performance counter due to partial trust scenarios
  169. // Disable the default performance counters' access otherwise
  170. // in PT the service will be broken
  171. PerformanceCounters.scope = PerformanceCounterScope.Off;
  172. DiagnosticUtility.TraceHandledException(new SecurityException(SR.GetString(
  173. SR.PartialTrustPerformanceCountersNotEnabled), securityException), TraceEventType.Warning);
  174. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  175. new SecurityException(SR.GetString(
  176. SR.PartialTrustPerformanceCountersNotEnabled)));
  177. }
  178. }
  179. #pragma warning suppress 56500 // covered by FxCOP
  180. catch (Exception e)
  181. {
  182. if (Fx.IsFatal(e))
  183. {
  184. throw;
  185. }
  186. if (null != counter)
  187. {
  188. if (!counter.ReadOnly)
  189. {
  190. try
  191. {
  192. counter.RemoveInstance();
  193. }
  194. // Already inside a catch block for a failure case
  195. // ok to ---- any exceptions here and trace the
  196. // original failure.
  197. #pragma warning suppress 56500 // covered by FxCOP
  198. catch (Exception e1)
  199. {
  200. if (Fx.IsFatal(e1))
  201. {
  202. throw;
  203. }
  204. }
  205. }
  206. counter = null;
  207. }
  208. bool logEvent = true;
  209. if (categoryName == PerformanceCounterStrings.SERVICEMODELSERVICE.ServicePerfCounters)
  210. {
  211. if (serviceOOM == false)
  212. {
  213. serviceOOM = true;
  214. }
  215. else
  216. {
  217. logEvent = false;
  218. }
  219. }
  220. else if (categoryName == PerformanceCounterStrings.SERVICEMODELOPERATION.OperationPerfCounters)
  221. {
  222. if (operationOOM == false)
  223. {
  224. operationOOM = true;
  225. }
  226. else
  227. {
  228. logEvent = false;
  229. }
  230. }
  231. else if (categoryName == PerformanceCounterStrings.SERVICEMODELENDPOINT.EndpointPerfCounters)
  232. {
  233. if (endpointOOM == false)
  234. {
  235. endpointOOM = true;
  236. }
  237. else
  238. {
  239. logEvent = false;
  240. }
  241. }
  242. if (logEvent)
  243. {
  244. DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
  245. (ushort)System.Runtime.Diagnostics.EventLogCategory.PerformanceCounter,
  246. (uint)System.Runtime.Diagnostics.EventLogEventId.FailedToLoadPerformanceCounter,
  247. categoryName,
  248. perfCounterName,
  249. e.ToString());
  250. }
  251. }
  252. return counter;
  253. }
  254. internal static Dictionary<string, ServiceModelPerformanceCounters> PerformanceCountersForEndpoint
  255. {
  256. get
  257. {
  258. if (PerformanceCounters.performanceCounters == null)
  259. {
  260. lock (PerformanceCounters.perfCounterDictionarySyncObject)
  261. {
  262. if (PerformanceCounters.performanceCounters == null)
  263. {
  264. PerformanceCounters.performanceCounters = new Dictionary<string, ServiceModelPerformanceCounters>();
  265. }
  266. }
  267. }
  268. return PerformanceCounters.performanceCounters;
  269. }
  270. }
  271. internal static List<ServiceModelPerformanceCounters> PerformanceCountersForEndpointList
  272. {
  273. get
  274. {
  275. if (PerformanceCounters.performanceCountersList == null)
  276. {
  277. lock (PerformanceCounters.perfCounterDictionarySyncObject)
  278. {
  279. if (PerformanceCounters.performanceCountersList == null)
  280. {
  281. PerformanceCounters.performanceCountersList = new List<ServiceModelPerformanceCounters>();
  282. }
  283. }
  284. }
  285. return PerformanceCounters.performanceCountersList;
  286. }
  287. }
  288. internal static Dictionary<string, ServiceModelPerformanceCountersEntry> PerformanceCountersForBaseUri
  289. {
  290. get
  291. {
  292. if (PerformanceCounters.performanceCountersBaseUri == null)
  293. {
  294. lock (PerformanceCounters.perfCounterDictionarySyncObject)
  295. {
  296. if (PerformanceCounters.performanceCountersBaseUri == null)
  297. {
  298. PerformanceCounters.performanceCountersBaseUri = new Dictionary<string, ServiceModelPerformanceCountersEntry>();
  299. }
  300. }
  301. }
  302. return PerformanceCounters.performanceCountersBaseUri;
  303. }
  304. }
  305. internal static void AddPerformanceCountersForEndpoint(
  306. ServiceHostBase serviceHost,
  307. ContractDescription contractDescription,
  308. EndpointDispatcher endpointDispatcher)
  309. {
  310. Fx.Assert(serviceHost != null, "The 'serviceHost' argument must not be null.");
  311. Fx.Assert(contractDescription != null, "The 'contractDescription' argument must not be null.");
  312. Fx.Assert(endpointDispatcher != null, "The 'endpointDispatcher' argument must not be null.");
  313. bool performanceCountersEnabled = PerformanceCounters.PerformanceCountersEnabled;
  314. bool minimalPerformanceCountersEnabled = PerformanceCounters.MinimalPerformanceCountersEnabled;
  315. if (performanceCountersEnabled || minimalPerformanceCountersEnabled)
  316. {
  317. if (endpointDispatcher.SetPerfCounterId())
  318. {
  319. ServiceModelPerformanceCounters counters;
  320. lock (PerformanceCounters.perfCounterDictionarySyncObject)
  321. {
  322. if (!PerformanceCounters.PerformanceCountersForEndpoint.TryGetValue(endpointDispatcher.PerfCounterId, out counters))
  323. {
  324. counters = new ServiceModelPerformanceCounters(serviceHost, contractDescription, endpointDispatcher);
  325. if (counters.Initialized)
  326. {
  327. PerformanceCounters.PerformanceCountersForEndpoint.Add(endpointDispatcher.PerfCounterId, counters);
  328. int index = PerformanceCounters.PerformanceCountersForEndpointList.FindIndex(c => c == null);
  329. if (index >= 0)
  330. {
  331. PerformanceCounters.PerformanceCountersForEndpointList[index] = counters;
  332. }
  333. else
  334. {
  335. PerformanceCounters.PerformanceCountersForEndpointList.Add(counters);
  336. index = PerformanceCounters.PerformanceCountersForEndpointList.Count - 1;
  337. }
  338. endpointDispatcher.PerfCounterInstanceId = index;
  339. }
  340. else
  341. {
  342. return;
  343. }
  344. }
  345. }
  346. ServiceModelPerformanceCountersEntry countersEntry;
  347. lock (PerformanceCounters.perfCounterDictionarySyncObject)
  348. {
  349. if (!PerformanceCounters.PerformanceCountersForBaseUri.TryGetValue(endpointDispatcher.PerfCounterBaseId, out countersEntry))
  350. {
  351. if (performanceCountersEnabled)
  352. {
  353. countersEntry = new ServiceModelPerformanceCountersEntry(serviceHost.Counters);
  354. }
  355. else if (minimalPerformanceCountersEnabled)
  356. {
  357. countersEntry = new ServiceModelPerformanceCountersEntry(serviceHost.DefaultCounters);
  358. }
  359. PerformanceCounters.PerformanceCountersForBaseUri.Add(endpointDispatcher.PerfCounterBaseId, countersEntry);
  360. }
  361. countersEntry.Add(counters);
  362. }
  363. }
  364. }
  365. }
  366. internal static void ReleasePerformanceCountersForEndpoint(string id, string baseId)
  367. {
  368. if (PerformanceCounters.PerformanceCountersEnabled)
  369. {
  370. lock (PerformanceCounters.perfCounterDictionarySyncObject)
  371. {
  372. if (!String.IsNullOrEmpty(id))
  373. {
  374. ServiceModelPerformanceCounters counters;
  375. if (PerformanceCounters.PerformanceCountersForEndpoint.TryGetValue(id, out counters))
  376. {
  377. PerformanceCounters.PerformanceCountersForEndpoint.Remove(id);
  378. int index = PerformanceCounters.PerformanceCountersForEndpointList.IndexOf(counters);
  379. PerformanceCounters.PerformanceCountersForEndpointList[index] = null;
  380. }
  381. }
  382. if (!String.IsNullOrEmpty(baseId))
  383. {
  384. PerformanceCounters.PerformanceCountersForBaseUri.Remove(baseId);
  385. }
  386. }
  387. }
  388. }
  389. internal static void ReleasePerformanceCounter(ref PerformanceCounter counter)
  390. {
  391. if (counter != null)
  392. {
  393. try
  394. {
  395. counter.RemoveInstance();
  396. counter = null;
  397. }
  398. #pragma warning suppress 56500 // covered by FxCOP
  399. catch (Exception e)
  400. {
  401. if (Fx.IsFatal(e))
  402. {
  403. throw;
  404. }
  405. }
  406. }
  407. }
  408. internal static void TxFlowed(EndpointDispatcher el, string operation)
  409. {
  410. if (null != el)
  411. {
  412. ServicePerformanceCountersBase sCounters = PerformanceCounters.GetServicePerformanceCounters(el.PerfCounterInstanceId);
  413. if (null != sCounters)
  414. {
  415. sCounters.TxFlowed();
  416. }
  417. if (PerformanceCounters.Scope == PerformanceCounterScope.All)
  418. {
  419. OperationPerformanceCountersBase oCounters = PerformanceCounters.GetOperationPerformanceCounters(el.PerfCounterInstanceId, operation);
  420. if (null != oCounters)
  421. {
  422. oCounters.TxFlowed();
  423. }
  424. EndpointPerformanceCountersBase eCounters = PerformanceCounters.GetEndpointPerformanceCounters(el.PerfCounterInstanceId);
  425. if (null != sCounters)
  426. {
  427. eCounters.TxFlowed();
  428. }
  429. }
  430. }
  431. }
  432. internal static void TxAborted(EndpointDispatcher el, long count)
  433. {
  434. if (PerformanceCounters.PerformanceCountersEnabled)
  435. {
  436. if (null != el)
  437. {
  438. ServicePerformanceCountersBase sCounters = PerformanceCounters.GetServicePerformanceCounters(el.PerfCounterInstanceId);
  439. if (null != sCounters)
  440. {
  441. sCounters.TxAborted(count);
  442. }
  443. }
  444. }
  445. }
  446. internal static void TxCommitted(EndpointDispatcher el, long count)
  447. {
  448. if (PerformanceCounters.PerformanceCountersEnabled)
  449. {
  450. if (null != el)
  451. {
  452. ServicePerformanceCountersBase sCounters = PerformanceCounters.GetServicePerformanceCounters(el.PerfCounterInstanceId);
  453. if (null != sCounters)
  454. {
  455. sCounters.TxCommitted(count);
  456. }
  457. }
  458. }
  459. }
  460. internal static void TxInDoubt(EndpointDispatcher el, long count)
  461. {
  462. if (PerformanceCounters.PerformanceCountersEnabled)
  463. {
  464. if (null != el)
  465. {
  466. ServicePerformanceCountersBase sCounters = PerformanceCounters.GetServicePerformanceCounters(el.PerfCounterInstanceId);
  467. if (null != sCounters)
  468. {
  469. sCounters.TxInDoubt(count);
  470. }
  471. }
  472. }
  473. }
  474. internal static void MethodCalled(string operationName)
  475. {
  476. EndpointDispatcher el = GetEndpointDispatcher();
  477. if (null != el)
  478. {
  479. if (PerformanceCounters.Scope == PerformanceCounterScope.All)
  480. {
  481. string uri = el.PerfCounterId;
  482. OperationPerformanceCountersBase opCounters = PerformanceCounters.GetOperationPerformanceCounters(el.PerfCounterInstanceId, operationName);
  483. if (null != opCounters)
  484. {
  485. opCounters.MethodCalled();
  486. }
  487. EndpointPerformanceCountersBase eCounters = PerformanceCounters.GetEndpointPerformanceCounters(el.PerfCounterInstanceId);
  488. if (null != eCounters)
  489. {
  490. eCounters.MethodCalled();
  491. }
  492. }
  493. ServicePerformanceCountersBase sCounters = PerformanceCounters.GetServicePerformanceCounters(el.PerfCounterInstanceId);
  494. if (null != sCounters)
  495. {
  496. sCounters.MethodCalled();
  497. }
  498. }
  499. }
  500. internal static void MethodReturnedSuccess(string operationName)
  501. {
  502. PerformanceCounters.MethodReturnedSuccess(operationName, -1);
  503. }
  504. internal static void MethodReturnedSuccess(string operationName, long time)
  505. {
  506. EndpointDispatcher el = GetEndpointDispatcher();
  507. if (null != el)
  508. {
  509. if (PerformanceCounters.Scope == PerformanceCounterScope.All)
  510. {
  511. string uri = el.PerfCounterId;
  512. OperationPerformanceCountersBase counters = PerformanceCounters.GetOperationPerformanceCounters(el.PerfCounterInstanceId, operationName);
  513. if (null != counters)
  514. {
  515. counters.MethodReturnedSuccess();
  516. if (time > 0)
  517. {
  518. counters.SaveCallDuration(time);
  519. }
  520. }
  521. EndpointPerformanceCountersBase eCounters = PerformanceCounters.GetEndpointPerformanceCounters(el.PerfCounterInstanceId);
  522. if (null != eCounters)
  523. {
  524. eCounters.MethodReturnedSuccess();
  525. if (time > 0)
  526. {
  527. eCounters.SaveCallDuration(time);
  528. }
  529. }
  530. }
  531. ServicePerformanceCountersBase sCounters = PerformanceCounters.GetServicePerformanceCounters(el.PerfCounterInstanceId);
  532. if (null != sCounters)
  533. {
  534. sCounters.MethodReturnedSuccess();
  535. if (time > 0)
  536. {
  537. sCounters.SaveCallDuration(time);
  538. }
  539. }
  540. }
  541. }
  542. internal static void MethodReturnedFault(string operationName)
  543. {
  544. PerformanceCounters.MethodReturnedFault(operationName, -1);
  545. }
  546. internal static void MethodReturnedFault(string operationName, long time)
  547. {
  548. EndpointDispatcher el = GetEndpointDispatcher();
  549. if (null != el)
  550. {
  551. if (PerformanceCounters.Scope == PerformanceCounterScope.All)
  552. {
  553. string uri = el.PerfCounterId;
  554. OperationPerformanceCountersBase counters = PerformanceCounters.GetOperationPerformanceCounters(el.PerfCounterInstanceId, operationName);
  555. if (null != counters)
  556. {
  557. counters.MethodReturnedFault();
  558. if (time > 0)
  559. {
  560. counters.SaveCallDuration(time);
  561. }
  562. }
  563. EndpointPerformanceCountersBase eCounters = PerformanceCounters.GetEndpointPerformanceCounters(el.PerfCounterInstanceId);
  564. if (null != eCounters)
  565. {
  566. eCounters.MethodReturnedFault();
  567. if (time > 0)
  568. {
  569. eCounters.SaveCallDuration(time);
  570. }
  571. }
  572. }
  573. ServicePerformanceCountersBase sCounters = PerformanceCounters.GetServicePerformanceCounters(el.PerfCounterInstanceId);
  574. if (null != sCounters)
  575. {
  576. sCounters.MethodReturnedFault();
  577. if (time > 0)
  578. {
  579. sCounters.SaveCallDuration(time);
  580. }
  581. }
  582. }
  583. }
  584. internal static void MethodReturnedError(string operationName)
  585. {
  586. PerformanceCounters.MethodReturnedError(operationName, -1);
  587. }
  588. internal static void MethodReturnedError(string operationName, long time)
  589. {
  590. EndpointDispatcher el = GetEndpointDispatcher();
  591. if (null != el)
  592. {
  593. if (PerformanceCounters.Scope == PerformanceCounterScope.All)
  594. {
  595. string uri = el.PerfCounterId;
  596. OperationPerformanceCountersBase counters = PerformanceCounters.GetOperationPerformanceCounters(el.PerfCounterInstanceId, operationName);
  597. if (null != counters)
  598. {
  599. counters.MethodReturnedError();
  600. if (time > 0)
  601. {
  602. counters.SaveCallDuration(time);
  603. }
  604. }
  605. EndpointPerformanceCountersBase eCounters = PerformanceCounters.GetEndpointPerformanceCounters(el.PerfCounterInstanceId);
  606. if (null != eCounters)
  607. {
  608. eCounters.MethodReturnedError();
  609. if (time > 0)
  610. {
  611. eCounters.SaveCallDuration(time);
  612. }
  613. }
  614. }
  615. ServicePerformanceCountersBase sCounters = PerformanceCounters.GetServicePerformanceCounters(el.PerfCounterInstanceId);
  616. if (null != sCounters)
  617. {
  618. sCounters.MethodReturnedError();
  619. if (time > 0)
  620. {
  621. sCounters.SaveCallDuration(time);
  622. }
  623. }
  624. }
  625. }
  626. static void InvokeMethod(object o, string methodName)
  627. {
  628. Fx.Assert(null != o, "object must not be null");
  629. MethodInfo method = o.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
  630. Fx.Assert(null != method, o.GetType().ToString() + " must have method " + methodName);
  631. method.Invoke(o, null);
  632. }
  633. static void CallOnAllCounters(string methodName, Message message, Uri listenUri, bool includeOperations)
  634. {
  635. Fx.Assert(null != message, "message must not be null");
  636. Fx.Assert(null != listenUri, "listenUri must not be null");
  637. if (null != message && null != message.Headers && null != message.Headers.To && null != listenUri)
  638. {
  639. string uri = listenUri.AbsoluteUri.ToUpperInvariant();
  640. ServiceModelPerformanceCountersEntry counters = PerformanceCounters.GetServiceModelPerformanceCountersBaseUri(uri);
  641. if (null != counters)
  642. {
  643. Fx.Assert(null != counters.ServicePerformanceCounters, "counters.ServicePerformanceCounters must not be null");
  644. PerformanceCounters.InvokeMethod(counters.ServicePerformanceCounters, methodName);
  645. if (PerformanceCounters.Scope == PerformanceCounterScope.All)
  646. {
  647. List<ServiceModelPerformanceCounters> counters2 = counters.CounterList;
  648. foreach (ServiceModelPerformanceCounters sCounters in counters2)
  649. {
  650. if (sCounters.EndpointPerformanceCounters != null)
  651. {
  652. PerformanceCounters.InvokeMethod(sCounters.EndpointPerformanceCounters, methodName);
  653. }
  654. if (includeOperations)
  655. {
  656. OperationPerformanceCountersBase oCounters = sCounters.GetOperationPerformanceCountersFromMessage(message);
  657. if (oCounters != null)
  658. {
  659. PerformanceCounters.InvokeMethod(oCounters, methodName);
  660. }
  661. }
  662. }
  663. }
  664. }
  665. }
  666. }
  667. static internal void AuthenticationFailed(Message message, Uri listenUri)
  668. {
  669. PerformanceCounters.CallOnAllCounters("AuthenticationFailed", message, listenUri, true);
  670. }
  671. static internal void AuthorizationFailed(string operationName)
  672. {
  673. EndpointDispatcher el = GetEndpointDispatcher();
  674. if (null != el)
  675. {
  676. string uri = el.PerfCounterId;
  677. if (PerformanceCounters.Scope == PerformanceCounterScope.All)
  678. {
  679. OperationPerformanceCountersBase counters = PerformanceCounters.GetOperationPerformanceCounters(el.PerfCounterInstanceId, operationName);
  680. if (null != counters)
  681. {
  682. counters.AuthorizationFailed();
  683. }
  684. EndpointPerformanceCountersBase eCounters = PerformanceCounters.GetEndpointPerformanceCounters(el.PerfCounterInstanceId);
  685. if (null != eCounters)
  686. {
  687. eCounters.AuthorizationFailed();
  688. }
  689. }
  690. ServicePerformanceCountersBase sCounters = PerformanceCounters.GetServicePerformanceCounters(el.PerfCounterInstanceId);
  691. if (null != sCounters)
  692. {
  693. sCounters.AuthorizationFailed();
  694. }
  695. }
  696. }
  697. internal static void SessionFaulted(string uri)
  698. {
  699. ServiceModelPerformanceCountersEntry counters = PerformanceCounters.GetServiceModelPerformanceCountersBaseUri(uri);
  700. if (null != counters)
  701. {
  702. counters.ServicePerformanceCounters.SessionFaulted();
  703. if (PerformanceCounters.Scope == PerformanceCounterScope.All)
  704. {
  705. List<ServiceModelPerformanceCounters> counters2 = counters.CounterList;
  706. foreach (ServiceModelPerformanceCounters sCounters in counters2)
  707. {
  708. if (sCounters.EndpointPerformanceCounters != null)
  709. {
  710. sCounters.EndpointPerformanceCounters.SessionFaulted();
  711. }
  712. }
  713. }
  714. }
  715. }
  716. internal static void MessageDropped(string uri)
  717. {
  718. ServiceModelPerformanceCountersEntry counters = PerformanceCounters.GetServiceModelPerformanceCountersBaseUri(uri);
  719. if (null != counters)
  720. {
  721. counters.ServicePerformanceCounters.MessageDropped();
  722. if (PerformanceCounters.Scope == PerformanceCounterScope.All)
  723. {
  724. List<ServiceModelPerformanceCounters> counters2 = counters.CounterList;
  725. foreach (ServiceModelPerformanceCounters sCounters in counters2)
  726. {
  727. if (sCounters.EndpointPerformanceCounters != null)
  728. {
  729. sCounters.EndpointPerformanceCounters.MessageDropped();
  730. }
  731. }
  732. }
  733. }
  734. }
  735. internal static void MsmqDroppedMessage(string uri)
  736. {
  737. if (PerformanceCounters.Scope == PerformanceCounterScope.All)
  738. {
  739. ServiceModelPerformanceCountersEntry counters = PerformanceCounters.GetServiceModelPerformanceCountersBaseUri(uri);
  740. if (null != counters)
  741. {
  742. counters.ServicePerformanceCounters.MsmqDroppedMessage();
  743. }
  744. }
  745. }
  746. internal static void MsmqPoisonMessage(string uri)
  747. {
  748. if (PerformanceCounters.Scope == PerformanceCounterScope.All)
  749. {
  750. ServiceModelPerformanceCountersEntry counters = PerformanceCounters.GetServiceModelPerformanceCountersBaseUri(uri);
  751. if (null != counters)
  752. {
  753. counters.ServicePerformanceCounters.MsmqPoisonMessage();
  754. }
  755. }
  756. }
  757. internal static void MsmqRejectedMessage(string uri)
  758. {
  759. if (PerformanceCounters.Scope == PerformanceCounterScope.All)
  760. {
  761. ServiceModelPerformanceCountersEntry counters = PerformanceCounters.GetServiceModelPerformanceCountersBaseUri(uri);
  762. if (null != counters)
  763. {
  764. counters.ServicePerformanceCounters.MsmqRejectedMessage();
  765. }
  766. }
  767. }
  768. static internal EndpointDispatcher GetEndpointDispatcher()
  769. {
  770. EndpointDispatcher endpointDispatcher = null;
  771. OperationContext currentContext = OperationContext.Current;
  772. if (null != currentContext && currentContext.InternalServiceChannel != null)
  773. {
  774. endpointDispatcher = currentContext.EndpointDispatcher;
  775. }
  776. return endpointDispatcher;
  777. }
  778. static ServiceModelPerformanceCounters GetServiceModelPerformanceCounters(int perfCounterInstanceId)
  779. {
  780. if (PerformanceCounters.PerformanceCountersForEndpointList.Count == 0)
  781. {
  782. return null;
  783. }
  784. return PerformanceCounters.PerformanceCountersForEndpointList[perfCounterInstanceId];
  785. }
  786. static ServiceModelPerformanceCountersEntry GetServiceModelPerformanceCountersBaseUri(string uri)
  787. {
  788. ServiceModelPerformanceCountersEntry counters = null;
  789. if (!String.IsNullOrEmpty(uri))
  790. {
  791. PerformanceCounters.PerformanceCountersForBaseUri.TryGetValue(uri, out counters);
  792. }
  793. return counters;
  794. }
  795. static OperationPerformanceCountersBase GetOperationPerformanceCounters(int perfCounterInstanceId, string operation)
  796. {
  797. ServiceModelPerformanceCounters counters = PerformanceCounters.GetServiceModelPerformanceCounters(perfCounterInstanceId);
  798. if (counters != null)
  799. {
  800. return counters.GetOperationPerformanceCounters(operation);
  801. }
  802. return null;
  803. }
  804. static EndpointPerformanceCountersBase GetEndpointPerformanceCounters(int perfCounterInstanceId)
  805. {
  806. ServiceModelPerformanceCounters counters = PerformanceCounters.GetServiceModelPerformanceCounters(perfCounterInstanceId);
  807. if (counters != null)
  808. {
  809. return counters.EndpointPerformanceCounters;
  810. }
  811. return null;
  812. }
  813. static ServicePerformanceCountersBase GetServicePerformanceCounters(int perfCounterInstanceId)
  814. {
  815. ServiceModelPerformanceCounters counters = PerformanceCounters.GetServiceModelPerformanceCounters(perfCounterInstanceId);
  816. if (counters != null)
  817. {
  818. return counters.ServicePerformanceCounters;
  819. }
  820. return null;
  821. }
  822. static internal void TracePerformanceCounterUpdateFailure(string instanceName, string perfCounterName)
  823. {
  824. if (DiagnosticUtility.ShouldTraceError)
  825. {
  826. TraceUtility.TraceEvent(
  827. System.Diagnostics.TraceEventType.Error,
  828. TraceCode.PerformanceCountersFailedDuringUpdate,
  829. SR.GetString(SR.TraceCodePerformanceCountersFailedDuringUpdate, perfCounterName + "::" + instanceName));
  830. }
  831. }
  832. }
  833. }