HyperPlatform Programmer's Reference
performance.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2017, Satoshi Tanda. All rights reserved.
2 // Use of this source code is governed by a MIT-style license that can be
3 // found in the LICENSE file.
4 
7 
8 #include "performance.h"
9 #include "common.h"
10 #include "log.h"
11 
13 //
14 // macro utilities
15 //
16 
18 //
19 // constants and macros
20 //
21 
23 //
24 // types
25 //
26 
28 //
29 // prototypes
30 //
31 
35 
36 #if defined(ALLOC_PRAGMA)
37 #pragma alloc_text(INIT, PerfInitialization)
38 #pragma alloc_text(PAGE, PerfTermination)
39 #endif
40 
42 //
43 // variables
44 //
45 
47 
49 //
50 // implementations
51 //
52 
53 _Use_decl_annotations_ NTSTATUS PerfInitialization() {
54  PAGED_CODE();
55  auto status = STATUS_SUCCESS;
56 
57  const auto perf_collector =
58  reinterpret_cast<PerfCollector*>(ExAllocatePoolWithTag(
59  NonPagedPool, sizeof(PerfCollector), kHyperPlatformCommonPoolTag));
60  if (!perf_collector) {
61  return STATUS_MEMORY_NOT_ALLOCATED;
62  }
63 
64  // No lock to avoid calling kernel APIs from VMM and race condition here is
65  // not an issue.
68 
69  g_performance_collector = perf_collector;
70  return status;
71 }
72 
73 _Use_decl_annotations_ void PerfTermination() {
74  PAGED_CODE();
75 
79  g_performance_collector = nullptr;
80  }
81 }
82 
83 /*_Use_decl_annotations_*/ ULONG64 PerfGetTime() {
84  LARGE_INTEGER counter = KeQueryPerformanceCounter(nullptr);
85  return static_cast<ULONG64>(counter.QuadPart);
86 }
87 
88 _Use_decl_annotations_ static void PerfpInitialOutputRoutine(
89  void* output_context) {
90  UNREFERENCED_PARAMETER(output_context);
91  HYPERPLATFORM_LOG_INFO("%-45s,%-20s,%-20s", "FunctionName(Line)",
92  "Execution Count", "Elapsed Time");
93 }
94 
95 _Use_decl_annotations_ static void PerfpOutputRoutine(
96  const char* location_name, ULONG64 total_execution_count,
97  ULONG64 total_elapsed_time, void* output_context) {
98  UNREFERENCED_PARAMETER(output_context);
99  HYPERPLATFORM_LOG_INFO("%-45s,%20I64u,%20I64u,", location_name,
100  total_execution_count, total_elapsed_time);
101 }
102 
103 _Use_decl_annotations_ static void PerfpFinalOutputRoutine(
104  void* output_context) {
105  UNREFERENCED_PARAMETER(output_context);
106 }
void Terminate()
Destructor; prints out accumulated performance results.
Definition: perf_counter.h:161
#define HYPERPLATFORM_LOG_INFO(format,...)
Definition: log.h:38
static const ULONG kHyperPlatformCommonPoolTag
A pool tag.
Definition: common.h:93
Declares interfaces to performance measurement functions.
PerfCollector * g_performance_collector
Stores all performance data collected by HYPERPLATFORM_PERFORMANCE_MEASURE_THIS_SCOPE().
Definition: performance.cpp:46
void Initialize(_In_ OutputRoutine *output_routine, _In_opt_ InitialOutputRoutine *initial_output_routine=NoOutputRoutine, _In_opt_ FinalOutputRoutine *final_output_routine=NoOutputRoutine, _In_opt_ LockRoutine *lock_enter_routine=NoLockRoutine, _In_opt_ LockRoutine *lock_leave_routine=NoLockRoutine, _In_opt_ void *lock_context=nullptr, _In_opt_ void *output_context=nullptr)
Constructor; call this only once before any other code in this module runs.
Definition: perf_counter.h:142
ULONG64 PerfGetTime()
Returns the current "time" for performance measurement.
Definition: performance.cpp:83
void(_In_ const char *location_name, _In_ ULONG64 total_execution_count, _In_ ULONG64 total_elapsed_time, _In_opt_ void *output_context) OutputRoutine
A function type for printing out results.
Definition: perf_counter.h:125
void(_In_opt_ void *output_context) FinalOutputRoutine
A function type for printing out a footer line of results.
Definition: perf_counter.h:119
static PerfCollector::OutputRoutine PerfpOutputRoutine
Definition: performance.cpp:33
NTSTATUS PerfInitialization()
Makes HYPERPLATFORM_PERFORMANCE_MEASURE_THIS_SCOPE() ready for use.
Definition: performance.cpp:53
void PerfTermination()
Ends performance monitoring and outputs its results.
Definition: performance.cpp:73
static PerfCollector::FinalOutputRoutine PerfpFinalOutputRoutine
Definition: performance.cpp:34
void(_In_opt_ void *output_context) InitialOutputRoutine
A function type for printing out a header line of results.
Definition: perf_counter.h:116
Declares and implements common things across the project.
Responsible for collecting and saving data supplied by PerfCounter.
Definition: perf_counter.h:113
Declares interfaces to logging functions.
static PerfCollector::InitialOutputRoutine PerfpInitialOutputRoutine
Definition: performance.cpp:32