- Main Page
- Modules
- Namespaces
- Classes
- Files

- Search for |

- File List
- File Members

# include/NvApplicationProfiler.h

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
    00003  *
    00004  * Redistribution and use in source and binary forms, with or without
    00005  * modification, are permitted provided that the following conditions
    00006  * are met:
    00007  *  * Redistributions of source code must retain the above copyright
    00008  *    notice, this list of conditions and the following disclaimer.
    00009  *  * Redistributions in binary form must reproduce the above copyright
    00010  *    notice, this list of conditions and the following disclaimer in the
    00011  *    documentation and/or other materials provided with the distribution.
    00012  *  * Neither the name of NVIDIA CORPORATION nor the names of its
    00013  *    contributors may be used to endorse or promote products derived
    00014  *    from this software without specific prior written permission.
    00015  *
    00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
    00017  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    00019  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    00020  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    00021  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    00022  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    00023  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    00024  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    00026  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00027  */
    00028
    00029 /**
    00030  * @file
    00031  * <b>NVIDIA Eagle Eye API: Application Resource Profiling API</b>
    00032  *
    00033  * @b Description: This file declares the NvApplicationProfiler API.
    00034  */
    00035 #ifndef __NV_PROFILER_H__
    00036 #define __NV_PROFILER_H__
    00037
    00038 #include <iostream>
    00039 #include <pthread.h>
    00040 #include <stdint.h>
    00041 #include <sys/time.h>
    00042 #include <time.h>
    00043
    00044 /**
    00045  *
    00046  * @c %NvApplicationProfiler is a helper class for profiling the overall application
    00047  * resource usage.
    00048  *
    00049  * NvApplicationProfiler spawns a background thread which periodically measures resource
    00050  * usage. This sampling interval can be configured. Smaller sampling intervals
    00051  * may lead to more accurate results but the background thread itself will
    00052  * have a higher CPU usage.
    00053  *
    00054  * Only one instance of NvApplicationProfiler object gets created for the application.
    00055  * It can be accessed using getProfilerInstance().
    00056  *
    00057  * NvApplicationProfiler currently samples CPU usage and provides peak and average CPU
    00058  * usage during the profiling duration. It requires that the CPU frequency be
    00059  * constant over the entire duration. To force this, NvApplicationProfiler will start only
    00060  * when the CPU governor is set to @b performance.
    00061  */
    00062 class NvApplicationProfiler
    00063 {
    00064 public:
    00065     /**
    00066      * Holds the profiling data
    00067      */
    00068     typedef struct
    00069     {
    00070         /** Total time for which the profiler ran / is running */
    00071         struct timeval total_time;
    00072         /** Peak CPU usage during the profiling time. */
    00073         float peak_cpu_usage;
    00074         /** Average CPU usage over the entire profiling duration. */
    00075         float avg_cpu_usage;
    00076         /** Number of cpu cores. */
    00077         uint32_t num_cpu_cores;
    00078         /** Operating frequency of the cpu in MHz. */
    00079         uint32_t cpu_freq_mhz;
    00080     } NvAppProfilerData;
    00081
    00082     static const uint64_t DefaultSamplingInterval = 100;
    00083
    00084     /**
    00085      * Get a reference to the global NvApplicationProfiler instance.
    00086      *
    00087      * @return A reference to the global NvApplicationProfiler instance.
    00088      */
    00089     static NvApplicationProfiler& getProfilerInstance();
    00090
    00091     /**
    00092      * Start the profiler with the specified sampling interval.
    00093      *
    00094      * This method resets the internal profiler data measurements.
    00095      *
    00096      * Starting an already started profiler does nothing.
    00097      *
    00098      * @param[in] sampling_interval_ms Sampling interval in milliseconds.
    00099      */
    00100     void start(uint32_t sampling_interval_ms);
    00101
    00102     /**
    00103      * Stop the profiler.
    00104      */
    00105     void stop();
    00106
    00107     /**
    00108      * Print the profiler data to an output stream.
    00109      *
    00110      * @param[in] outstream A reference to an output stream of type std::ostream.
    00111      *                      std::cout if not specified.
    00112      */
    00113     void printProfilerData(std::ostream &outstream = std::cout);
    00114
    00115     /**
    00116      * Get the profiler data.
    00117      *
    00118      * @param[out] data Pointer to the ProfilerData structure to be filled.
    00119      */
    00120     void getProfilerData(NvAppProfilerData &data);
    00121
    00122 private:
    00123     /**
    00124      * Method which is run by the background profiling thread.
    00125      *
    00126      * Waits on a condition till the next periodic sampling time and calls
    00127      * profile() for measurement.
    00128      *
    00129      * Runs in an infinite loop till signaled to stop.
    00130      */
    00131     static void * ProfilerThread(void *);
    00132
    00133     bool running;  /**< Boolean flag indicating if profiling thread is running. */
    00134     uint32_t sampling_interval; /**< Interval between two measurements,
    00135                                      in milliseconds. */
    00136
    00137     pthread_mutex_t thread_lock; /**< Lock for synchronized multithreaded
    00138                                       access to #data */
    00139     pthread_t profiling_thread;  /** ID of the profiling thread running in
    00140                                      background */
    00141
    00142     uint32_t num_cpu_cores; /**< Number of CPU cores. */
    00143     uint32_t cpu_freq; /**< Operating frequency of CPU cores in MHz. */
    00144     bool check_cpu_usage; /**< Flag indicating if cpu usage should be checked. */
    00145
    00146     /**
    00147      * Internal structure to hold resource usage readings.
    00148      */
    00149     struct ProfilerDataInternal
    00150     {
    00151         /** Wall-clock time at which profiler was started. */
    00152         struct timeval start_time;
    00153         /** Wall-clock time at which profiler was stopped. */
    00154         struct timeval stop_time;
    00155
    00156         /** CPU clock time occupied by the process when the
    00157          *  profiler was started. */
    00158         struct timespec start_proc_cpu_clock_time;
    00159         /** Total CPU clock time when the profiler was started. */
    00160         struct timespec start_cpu_clock_time;
    00161
    00162         /** CPU clock time occupied by the process when the
    00163          *  latest readings were taken. */
    00164         struct timespec stop_proc_cpu_clock_time;
    00165         /** Total CPU clock time when the latest readings were taken. */
    00166         struct timespec stop_cpu_clock_time;
    00167
    00168         /** Maximum of CPU usages of all sampled periods. */
    00169         float max_cpu_usage;
    00170         /** Minimum of CPU usages of all sampled periods. */
    00171         float min_cpu_usage;
    00172         /** Average CPU usage over the entire profiling duration. */
    00173         float avg_cpu_usage;
    00174
    00175         /** Number of readings taken. */
    00176         uint64_t num_readings;
    00177     } data; /**< Internal structure to hold intermediate measurements. */
    00178
    00179     /**
    00180      * Measure resource usage parameters.
    00181      */
    00182     void profile();
    00183
    00184     /**
    00185      * Default constructor used by getProfilerInstance.
    00186      */
    00187     NvApplicationProfiler();
    00188
    00189     /**
    00190      * Disallow copy constructor.
    00191      */
    00192     NvApplicationProfiler(const NvApplicationProfiler& that);
    00193     /**
    00194      * Disallow assignment.
    00195      */
    00196     void operator=(NvApplicationProfiler const&);
    00197 };
    00198
    00199 #endif