2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-02-06 02:49:37 +04:00
|
|
|
|
|
|
|
/*****************************
|
|
|
|
Windows implementation of probes, using xperf
|
|
|
|
*****************************/
|
|
|
|
#include <windows.h>
|
|
|
|
#include <wmistr.h>
|
|
|
|
#include <evntrace.h>
|
|
|
|
|
|
|
|
#include "perfprobe.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace probes {
|
|
|
|
|
|
|
|
#if defined(MOZ_LOGGING)
|
2015-10-19 22:50:14 +03:00
|
|
|
static LazyLogModule sProbeLog("SysProbe");
|
|
|
|
#define LOG(x) MOZ_LOG(sProbeLog, mozilla::LogLevel::Debug, x)
|
2012-02-06 02:49:37 +04:00
|
|
|
#else
|
|
|
|
#define LOG(x)
|
|
|
|
#endif
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
// Utility function
|
|
|
|
GUID
|
|
|
|
CID_to_GUID(const nsCID& aCID)
|
2012-02-06 02:49:37 +04:00
|
|
|
{
|
|
|
|
GUID result;
|
|
|
|
result.Data1 = aCID.m0;
|
|
|
|
result.Data2 = aCID.m1;
|
|
|
|
result.Data3 = aCID.m2;
|
2014-08-13 22:45:37 +04:00
|
|
|
for (int i = 0; i < 8; ++i) {
|
2012-02-06 02:49:37 +04:00
|
|
|
result.Data4[i] = aCID.m3[i];
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2012-02-06 02:49:37 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Implementation of Probe
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
Probe::Probe(const nsCID& aGUID,
|
|
|
|
const nsACString& aName,
|
|
|
|
ProbeManager* aManager)
|
2012-02-06 02:49:37 +04:00
|
|
|
: mGUID(CID_to_GUID(aGUID))
|
|
|
|
, mName(aName)
|
|
|
|
, mManager(aManager)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
nsresult
|
|
|
|
Probe::Trigger()
|
2012-02-06 02:49:37 +04:00
|
|
|
{
|
|
|
|
if (!(mManager->mIsActive)) {
|
|
|
|
//Do not trigger if there is no session
|
2014-08-13 22:45:37 +04:00
|
|
|
return NS_OK;
|
2012-02-06 02:49:37 +04:00
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
|
2012-02-06 02:49:37 +04:00
|
|
|
_EVENT_TRACE_HEADER event;
|
|
|
|
ZeroMemory(&event, sizeof(event));
|
|
|
|
event.Size = sizeof(event);
|
|
|
|
event.Flags = WNODE_FLAG_TRACED_GUID ;
|
|
|
|
event.Guid = (const GUID)mGUID;
|
|
|
|
event.Class.Type = 1;
|
|
|
|
event.Class.Version = 0;
|
|
|
|
event.Class.Level = TRACE_LEVEL_INFORMATION;
|
2014-08-13 22:45:37 +04:00
|
|
|
|
2012-02-06 02:49:37 +04:00
|
|
|
ULONG result = TraceEvent(mManager->mSessionHandle, &event);
|
2014-08-13 22:45:37 +04:00
|
|
|
|
2012-02-06 02:49:37 +04:00
|
|
|
LOG(("Probes: Triggered %s, %s, %ld",
|
|
|
|
mName.Data(),
|
2014-08-13 22:45:37 +04:00
|
|
|
result == ERROR_SUCCESS ? "success" : "failure",
|
2012-02-06 02:49:37 +04:00
|
|
|
result));
|
2014-08-13 22:45:37 +04:00
|
|
|
|
2012-02-06 02:49:37 +04:00
|
|
|
nsresult rv;
|
2014-08-13 22:45:37 +04:00
|
|
|
switch (result) {
|
|
|
|
case ERROR_SUCCESS:
|
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
2012-02-06 02:49:37 +04:00
|
|
|
case ERROR_INVALID_FLAG_NUMBER:
|
|
|
|
case ERROR_MORE_DATA:
|
2014-08-13 22:45:37 +04:00
|
|
|
case ERROR_INVALID_PARAMETER:
|
|
|
|
rv = NS_ERROR_INVALID_ARG;
|
|
|
|
break;
|
|
|
|
case ERROR_INVALID_HANDLE:
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
break;
|
2012-02-06 02:49:37 +04:00
|
|
|
case ERROR_NOT_ENOUGH_MEMORY:
|
2014-08-13 22:45:37 +04:00
|
|
|
case ERROR_OUTOFMEMORY:
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
2012-02-06 02:49:37 +04:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Implementation of ProbeManager
|
|
|
|
|
|
|
|
ProbeManager::~ProbeManager()
|
|
|
|
{
|
|
|
|
//If the manager goes out of scope, stop the session.
|
|
|
|
if (mIsActive && mRegistrationHandle) {
|
|
|
|
StopSession();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
ProbeManager::ProbeManager(const nsCID& aApplicationUID,
|
|
|
|
const nsACString& aApplicationName)
|
2015-07-24 12:45:50 +03:00
|
|
|
: mIsActive(false)
|
|
|
|
, mApplicationUID(aApplicationUID)
|
2012-02-06 02:49:37 +04:00
|
|
|
, mApplicationName(aApplicationName)
|
2012-02-08 14:53:52 +04:00
|
|
|
, mSessionHandle(0)
|
|
|
|
, mRegistrationHandle(0)
|
2015-07-24 12:45:50 +03:00
|
|
|
, mInitialized(false)
|
2012-02-06 02:49:37 +04:00
|
|
|
{
|
|
|
|
#if defined(MOZ_LOGGING)
|
|
|
|
char cidStr[NSID_LENGTH];
|
|
|
|
aApplicationUID.ToProvidedString(cidStr);
|
|
|
|
LOG(("ProbeManager::Init for application %s, %s",
|
|
|
|
aApplicationName.Data(), cidStr));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//Note: The Windows API is just a little bit scary there.
|
|
|
|
//The only way to obtain the session handle is to
|
|
|
|
//- ignore the session handle obtained from RegisterTraceGuids
|
|
|
|
//- pass a callback
|
|
|
|
//- in that callback, request the session handle through
|
|
|
|
// GetTraceLoggerHandle and some opaque value received by the callback
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
ULONG WINAPI
|
|
|
|
ControlCallback(WMIDPREQUESTCODE aRequestCode,
|
|
|
|
PVOID aContext,
|
|
|
|
ULONG* aReserved,
|
|
|
|
PVOID aBuffer)
|
2012-02-06 02:49:37 +04:00
|
|
|
{
|
2014-08-13 22:45:37 +04:00
|
|
|
ProbeManager* context = (ProbeManager*)aContext;
|
|
|
|
switch (aRequestCode) {
|
|
|
|
case WMI_ENABLE_EVENTS: {
|
2012-02-06 02:49:37 +04:00
|
|
|
context->mIsActive = true;
|
2014-08-13 22:45:37 +04:00
|
|
|
TRACEHANDLE sessionHandle = GetTraceLoggerHandle(aBuffer);
|
2012-02-06 02:49:37 +04:00
|
|
|
//Note: We only accept one handle
|
|
|
|
if ((HANDLE)sessionHandle == INVALID_HANDLE_VALUE) {
|
|
|
|
ULONG result = GetLastError();
|
|
|
|
LOG(("Probes: ControlCallback failed, %ul", result));
|
|
|
|
return result;
|
2014-08-13 22:45:37 +04:00
|
|
|
} else if (context->mIsActive && context->mSessionHandle &&
|
|
|
|
context->mSessionHandle != sessionHandle) {
|
2012-02-06 02:49:37 +04:00
|
|
|
LOG(("Probes: Can only handle one context at a time, "
|
|
|
|
"ignoring activation"));
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
} else {
|
|
|
|
context->mSessionHandle = sessionHandle;
|
|
|
|
LOG(("Probes: ControlCallback activated"));
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
case WMI_DISABLE_EVENTS:
|
|
|
|
context->mIsActive = false;
|
|
|
|
context->mSessionHandle = 0;
|
|
|
|
LOG(("Probes: ControlCallback deactivated"));
|
|
|
|
return ERROR_SUCCESS;
|
2012-02-06 02:49:37 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
default:
|
|
|
|
LOG(("Probes: ControlCallback does not know what to do with %d",
|
|
|
|
aRequestCode));
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
2012-02-06 02:49:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
already_AddRefed<Probe>
|
|
|
|
ProbeManager::GetProbe(const nsCID& aEventUID, const nsACString& aEventName)
|
2012-02-06 02:49:37 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Probe> result(new Probe(aEventUID, aEventName, this));
|
2012-02-06 02:49:37 +04:00
|
|
|
mAllProbes.AppendElement(result);
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
nsresult
|
|
|
|
ProbeManager::StartSession()
|
2012-02-06 02:49:37 +04:00
|
|
|
{
|
|
|
|
return StartSession(mAllProbes);
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
nsresult
|
2015-10-18 08:24:48 +03:00
|
|
|
ProbeManager::StartSession(nsTArray<RefPtr<Probe>>& aProbes)
|
2012-02-06 02:49:37 +04:00
|
|
|
{
|
|
|
|
const size_t probesCount = aProbes.Length();
|
|
|
|
_TRACE_GUID_REGISTRATION* probes = new _TRACE_GUID_REGISTRATION[probesCount];
|
|
|
|
for (unsigned int i = 0; i < probesCount; ++i) {
|
2014-08-13 22:45:37 +04:00
|
|
|
const Probe* probe = aProbes[i];
|
|
|
|
const Probe* probeX = static_cast<const Probe*>(probe);
|
|
|
|
probes[i].Guid = (LPCGUID)&probeX->mGUID;
|
2012-02-06 02:49:37 +04:00
|
|
|
}
|
|
|
|
ULONG result =
|
|
|
|
RegisterTraceGuids(&ControlCallback
|
2014-08-13 22:45:37 +04:00
|
|
|
/*RequestAddress: Sets mSessions appropriately.*/,
|
|
|
|
this
|
|
|
|
/*RequestContext: Passed to ControlCallback*/,
|
|
|
|
(LPGUID)&mApplicationUID
|
|
|
|
/*ControlGuid: Tracing GUID
|
|
|
|
the cast comes from MSDN examples*/,
|
|
|
|
probesCount
|
|
|
|
/*GuidCount: Number of probes*/,
|
|
|
|
probes
|
|
|
|
/*TraceGuidReg: Probes registration*/,
|
|
|
|
nullptr
|
|
|
|
/*MofImagePath: Must be nullptr, says MSDN*/,
|
|
|
|
nullptr
|
|
|
|
/*MofResourceName:Must be nullptr, says MSDN*/,
|
|
|
|
&mRegistrationHandle
|
|
|
|
/*RegistrationHandle: Handler.
|
|
|
|
used only for unregistration*/
|
|
|
|
);
|
2012-02-06 02:49:37 +04:00
|
|
|
delete[] probes;
|
2014-08-13 22:45:37 +04:00
|
|
|
if (NS_WARN_IF(result != ERROR_SUCCESS)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2012-02-06 02:49:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
nsresult
|
|
|
|
ProbeManager::StopSession()
|
2012-02-06 02:49:37 +04:00
|
|
|
{
|
|
|
|
LOG(("Probes: Stopping measures"));
|
2012-02-08 14:53:52 +04:00
|
|
|
if (mSessionHandle != 0) {
|
2012-02-06 02:49:37 +04:00
|
|
|
ULONG result = UnregisterTraceGuids(mSessionHandle);
|
2012-02-08 14:53:52 +04:00
|
|
|
mSessionHandle = 0;
|
2012-02-06 02:49:37 +04:00
|
|
|
if (result != ERROR_SUCCESS) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace probes
|
|
|
|
} // namespace mozilla
|