xpcom/build/perfprobe.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/build/perfprobe.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,203 @@
     1.4 +/* -*-  Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/**
    1.10 + * A mechanism for interacting with operating system-provided
    1.11 + * debugging/profiling tools such as Microsoft EWT/Windows Performance Toolkit.
    1.12 + */
    1.13 +
    1.14 +#ifndef mozilla_perfprobe_h
    1.15 +#define mozilla_perfprobe_h
    1.16 +
    1.17 +#if !defined(XP_WIN)
    1.18 +#error "For the moment, perfprobe.h is defined only for Windows platforms"
    1.19 +#endif
    1.20 +
    1.21 +#include "nsError.h"
    1.22 +#include "nsString.h"
    1.23 +#include "prlog.h"
    1.24 +#include "nsTArray.h"
    1.25 +#include "nsAutoPtr.h"
    1.26 +#include <windows.h>
    1.27 +#undef GetStartupInfo //Prevent Windows from polluting global namespace
    1.28 +#include <wmistr.h>
    1.29 +#include <evntrace.h>
    1.30 +
    1.31 +namespace mozilla {
    1.32 +namespace probes {
    1.33 +
    1.34 +class ProbeManager;
    1.35 +
    1.36 +/**
    1.37 + * A data structure supporting a trigger operation that can be used to
    1.38 + * send information to the operating system.
    1.39 + */
    1.40 +
    1.41 +class Probe
    1.42 +{
    1.43 +public:
    1.44 +  NS_INLINE_DECL_REFCOUNTING(Probe)
    1.45 +
    1.46 +  /**
    1.47 +   * Trigger the event.
    1.48 +   *
    1.49 +   * Note: Can be called from any thread.
    1.50 +   */
    1.51 +  nsresult Trigger();
    1.52 +  ~Probe() {};
    1.53 +
    1.54 +protected:
    1.55 +  Probe(const nsCID &aGUID,
    1.56 +        const nsACString &aName,
    1.57 +         ProbeManager *aManager);
    1.58 +  friend class ProbeManager;
    1.59 +
    1.60 +protected:
    1.61 +
    1.62 +  /**
    1.63 +   * The system GUID associated to this probe. See the documentation
    1.64 +   * of |ProbeManager::Make| for more details.
    1.65 +   */
    1.66 +  const GUID mGUID;
    1.67 +
    1.68 +  /**
    1.69 +   * The name of this probe. See the documentation
    1.70 +   * of |ProbeManager::Make| for more details.
    1.71 +   */
    1.72 +  const nsCString mName;
    1.73 +
    1.74 +  /**
    1.75 +   * The ProbeManager managing this probe.
    1.76 +   *
    1.77 +   * Note: This is a weak reference to avoid a useless cycle.
    1.78 +   */
    1.79 +  class ProbeManager *mManager;
    1.80 +};
    1.81 +
    1.82 +
    1.83 +/**
    1.84 + * A manager for a group of probes.
    1.85 + *
    1.86 + * You can have several managers in one application, provided that they all
    1.87 + * have distinct IDs and names. However, having more than 2 is considered a bad
    1.88 + * practice.
    1.89 + */
    1.90 +class ProbeManager
    1.91 +{
    1.92 +public:
    1.93 +  NS_INLINE_DECL_REFCOUNTING(ProbeManager)
    1.94 +
    1.95 +  /**
    1.96 +   * Create a new probe manager.
    1.97 +   *
    1.98 +   * This constructor should be called from the main thread.
    1.99 +   *
   1.100 +   * @param uid The unique ID of the probe. Under Windows, this unique
   1.101 +   * ID must have been previously registered using an external tool.
   1.102 +   * See MyCategory on http://msdn.microsoft.com/en-us/library/aa364100.aspx
   1.103 +   * @param name A name for the probe. Currently used only for logging purposes.
   1.104 +   * In the future, may be attached to the data sent to the operating system.
   1.105 +   *
   1.106 +   * Note: If two ProbeManagers are constructed with the same uid and/or name,
   1.107 +   * behavior is unspecified.
   1.108 +   */
   1.109 +  ProbeManager(const nsCID &applicationUID,
   1.110 +               const nsACString &applicationName);
   1.111 +
   1.112 +  /**
   1.113 +   * Acquire a probe.
   1.114 +   *
   1.115 +   * Note: Only probes acquired before the call to SetReady are taken into
   1.116 +   * account
   1.117 +   * Note: Can be called only from the main thread.
   1.118 +   *
   1.119 +   * @param eventUID The unique ID of the probe. Under Windows, this unique
   1.120 +   * ID must have been previously registered using an external tool.
   1.121 +   * See MyCategory on http://msdn.microsoft.com/en-us/library/aa364100.aspx
   1.122 +   * @param eventMame A name for the probe. Currently used only for logging
   1.123 +   * purposes. In the
   1.124 +   * future, may be attached to the data sent to the operating system.
   1.125 +   * @return Either |null| in case of error or a valid |Probe*|.
   1.126 +   *
   1.127 +   * Note: If this method is called twice with the same uid and/or name,
   1.128 +   * behavior is undefined.
   1.129 +   */
   1.130 +  already_AddRefed<Probe> GetProbe(const nsCID &eventUID,
   1.131 +                                   const nsACString &eventName);
   1.132 +
   1.133 +  /**
   1.134 +   * Start/stop the measuring session.
   1.135 +   *
   1.136 +   * This method should be called from the main thread.
   1.137 +   *
   1.138 +   * Note that starting an already started probe manager has no effect,
   1.139 +   * nor does stopping an already stopped probe manager.
   1.140 +   */
   1.141 +  nsresult StartSession();
   1.142 +  nsresult StopSession();
   1.143 +
   1.144 +  /**
   1.145 +   * @return true If measures are currently on, i.e. if triggering probes is any
   1.146 +   * is useful. You do not have to check this before triggering a probe, unless
   1.147 +   * this can avoid complex computations.
   1.148 +   */
   1.149 +  bool IsActive();
   1.150 +
   1.151 +  ~ProbeManager();
   1.152 +
   1.153 +protected:
   1.154 +  nsresult StartSession(nsTArray<nsRefPtr<Probe> > &probes);
   1.155 +  nsresult Init(const nsCID &applicationUID, const nsACString &applicationName);
   1.156 +
   1.157 +protected:
   1.158 +  /**
   1.159 +   * `true` if a session is in activity, `false` otherwise.
   1.160 +   */
   1.161 +  bool mIsActive;
   1.162 +
   1.163 +  /**
   1.164 +   * The UID of this manager.
   1.165 +   * See documentation above for registration steps that you
   1.166 +   * may have to take.
   1.167 +   */
   1.168 +  nsCID mApplicationUID;
   1.169 +
   1.170 +  /**
   1.171 +   * The name of the application.
   1.172 +   */
   1.173 +  nsCString mApplicationName;
   1.174 +
   1.175 +  /**
   1.176 +   * All the probes that have been created for this manager.
   1.177 +   */
   1.178 +  nsTArray<nsRefPtr<Probe> > mAllProbes;
   1.179 +
   1.180 +  /**
   1.181 +   * Handle used for triggering events
   1.182 +   */
   1.183 +  TRACEHANDLE mSessionHandle;
   1.184 +
   1.185 +  /**
   1.186 +   * Handle used for registration/unregistration
   1.187 +   */
   1.188 +  TRACEHANDLE mRegistrationHandle;
   1.189 +
   1.190 +  /**
   1.191 +   * `true` if initialization has been performed, `false` until then.
   1.192 +   */
   1.193 +  bool mInitialized;
   1.194 +
   1.195 +  friend class Probe;//Needs to access |mSessionHandle|
   1.196 +  friend ULONG WINAPI ControlCallback(
   1.197 +                                      WMIDPREQUESTCODE RequestCode,
   1.198 +                                      PVOID Context,
   1.199 +                                      ULONG *Reserved,
   1.200 +                                      PVOID Buffer
   1.201 +                                      );//Sets |mSessionHandle|
   1.202 +};
   1.203 +}
   1.204 +}
   1.205 +
   1.206 +#endif //mozilla_perfprobe_h

mercurial