GithubHelp home page GithubHelp logo

zosrothko / penter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from osrdrivers/penter

0.0 0.0 0.0 25 KB

penter hook example and driver time recorder

License: BSD 3-Clause "New" or "Revised" License

C 51.81% C++ 32.33% Assembly 15.86%

penter's Introduction

About penter

Many years ago, we were working on a file system filter driver that would absolutely destroy the performance of a system under certain workloads. Operations that previously only took a few minutes would turn into hours. We were also pretty convinced that there were some workloads that would now literally take "forever", though we never had the patience to actually wait that long.

The project was incredibly complex and we were having difficulty narrowing down the exact reason for the degradation. CPU utilization was low. Disk utilization was low. We even started removing locks to see if being wrong could make us faster (it didn't).

After scratching our heads for a bit, we decided that it might be useful to see the aggregate elapsed times of all the functions in our filter suite. Given that we had such a disparity in elapsed time when our filter was involved, we assumed that we should be able to get better insight into what was going on if we could find "fat" functions.

Thus, the penter tracing library was born. If you compile your code using the VisualC compiler's /Gh and /GH switches, calls to the _penter and _pexit functions will be embedded at the start and end of each function in your module. By writing our own _penter and _pexit hooks, we kept track of the amount of time spent in each function of the driver. We then created a WinDbg debugger extension to extract the data into a CSV format that we could easily import into Excel.

We didn't build it to be pretty. We didn't build it to be fast. BUT, it did absolutely help us narrow down what the problems were (they were flushes!). We also had quite a difficult time getting the penter hooks to work on x64. So, we thought we'd share the code in case it's useful for someone else (either to track down a problem or as an example of a penter hook on x64).

Building penter Library and Debugger Extension

The provided solution builds the penterlib static library containing the _penter and _pexit hooks. The code supports Debug and Release builds on both the x86 and x64.

It also builds the penterkd.dll WinDbg debugger extension. This extension will be used to collect the trace data from the target system.

Adding penter Tracing to a Project

If you want to add penter support to a driver project add the /Gh and /GH compiler options. Once you do so you will receive errors about _penter and _pexit not being defined for your module. Adding the penterlib.lib file as a library dependency will then resolve the compilation errors.

The repo also provides a penter.props file that you can include in your vcxproj to automatically set the appropriate compile and link flags.

Extracting Trace Information

Once your driver is compiled with the necessary hooks, load the penterkd Debugger Extension on your host machine:

0: kd> !load E:\repos\Developer\SNoone\penter\x64\Debug\penterkd.dll

Now you can dump out the trace data to collect statistics on the module:

0: kd> !modulestats scanner
Function,CallCount,CallTicks,TicksPerCall
scanner!DriverEntry,1,4715,4715
scanner!ExInitializeDriverRuntime,1,37,37
scanner!ScannerInitializeScannedExtensions,1,333,333
scanner!KeGetCurrentIrql,4298,621,0
scanner!ScannerAllocateUnicodeString,6,113,18
scanner!ScannerInstanceSetup,9,173,19
scanner!ScannerPreCleanup,3389,2843,0
scanner!ScannerPreCreate,4281,98602,23
scanner!ScannerPostCreate,4281,4189123,978
scanner!ScannerPreFileSystemControl,969,1318,1
scanner!ScannerpCheckExtension,3422,6356,1
scanner!ScannerPreWrite,186,143,0
scanner!ScannerpScanFileInUserMode,203,3485008,17167
scanner!ScannerPortConnect,1,22,22

The data can now be easily imported into Excel. Generally the column of most interest is TicksPerCall, which is the total number of ticks used by the function divided by the number of calls to the function. The bigger the number the more time you're spending in that function.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.