Integrating Profiling Helper Tool code to an existing application

To use Profiling Helper Tool with an already existing Kanzi application that you created without one of the Profiling Helper Tool templates, you must manually integrate Profiling Helper Tool to your application.

Integrating Profiling Helper Tool code to an existing Visual Studio application

To integrate Profiling Helper Tool code to an existing Visual Studio application:

  1. Copy the Profiling Helper Tool <ProfilingHelperTool>/Application/src/plugin directory to the Application/src directory of your Kanzi application.

    If you use a different directory for source files in your application, copy the plugin directory there.

  2. Open the CMakeLists.txt file of your Kanzi application and add Profiling Helper Tool as a dependency:

    add_subdirectory(src/plugin)
    
    ...
    
    target_link_libraries(MyApplication Kanzi::kzui Kanzi::kzcoreui ProfilingHelper)
    
  3. In the application.cfg file of your Kanzi application, enable all profilers.

    Enabling the profilers does not affect performance on Debug or Release builds, because profiling measurements are used only in the Profiling build.

    # Enables all performance profiling categories.
    ProfilingCategoryFilter="*=on"
    
  4. In your application class implementation, add the headers for Profiling Helper Tool.

    #include "profilinghelper_collector.hpp"
    #include "profiling_helper.hpp"
    
  5. In your application class implementation, add Profiling Helper Tool to the module list under Application::onConfigure function.

    // Configures application.
    void onConfigure(ApplicationProperties& configuration) override
    {
       ...
    
       // Add the ProfilingHelper Module to load it. The ProfilingHelper module needs to be available as a DLL.
       configuration.moduleNames.push_back("ProfilingHelper");
    
       ...
    }
    
  6. (Optional) In your application class implementation, register startup profilers and set up FPS profiler in the Application::registerMetadataOverride function.

    void registerMetadataOverride(ObjectFactory& factory) override
    {
       ...
    
       // Register the Application's startup profilers with the SampleCollector.
       profilinghelper::SampleCollector::setStartupProfilers(getStartupProfilerRegistry());
    
       // Set up the Application's FPS profiler with the ProfilingHelper.
       profilinghelper::ProfilingHelper::setPerformanceInfoFPSProfiler(this);
    
       ...
    }
    
  7. In Visual Studio, set the build configuration to Profiling.

    This way you enable Kanzi Engine to collect profiling data at runtime.

Integrating Profiling Helper Tool code to an existing Kanzi Android framework application

To integrate Profiling Helper Tool source code to an existing Kanzi Android framework application:

  1. Copy these files and directories to your Kanzi application:

    • <ProfilingHelperTool>/Application/configs/platforms/droidfw_gradle/profilinghelperplugin directory to the Application/configs/platforms/android_gradle directory of your Kanzi application

    • <ProfilingHelperTool>/Application/src/plugin directory to the Application/src directory of your Kanzi application

    • <ProfilingHelperTool>/Application/bin/profilinghelper.cfg to the Application/bin directory of your application

  2. In the Application/configs/platforms/android_gradle/settings.gradle, include the profilinghelperplugin project.

    include ':profilinghelperplugin'
    
  3. In the Application/configs/platforms/android_gradle/app/build.gradle, add the profilinghelperplugin project as a dependency.

    dependencies {
       implementation project(':profilinghelperplugin')
    
  4. In the application.cfg file of your application, enable all profilers and add the ProfilingHelper to the list of modules.

    Enabling the profilers does not affect application performance of the Debug or Release builds, because profiling measurements are performed only in the Profiling build.

    # Enables all performance profiling categories.
    ProfilingCategoryFilter="*=on"
    ModuleNames = "ProfilingHelper"
    
  5. To save the profiling data to the filesystem, in the Application/configs/platforms/android_gradle/app/src/main/AndroidManifest.xml, enable the read and write permission.

    • For Android SDK version 30 and newer:

      <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
      
    • For Android SDK version older than 30:

      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
      
  6. Create a pop-up when the application starts where you ask for file system access permission. Android requires users to confirm permission requests. This approach makes it easier to enable permissions than it is to do so manually in the application settings.

    1. In the Java file that contains the main activity of your application, add the requestReadWritePermissions function.

      • For Android SDK version 30 and newer:

        void requestReadWritePermissions() {
           // Check and request permission to read and write to external storage.
           // Android SDK 30 and above requires MANAGE_EXTERNAL_STORAGE.
           if(SDK_INT >= Build.VERSION_CODES.R) {
              if (!Environment.isExternalStorageManager()) {
                    Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
                    Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri);
                    startActivity(intent);
              }
           }
        }
        
      • For Android SDK version older than 30:

        void requestReadWritePermissions() {
           // Check and request permission to read and write to external storage.
           // Android SDK 30 and above requires WRITE_EXTERNAL_STORAGE.
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
              if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    ||checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
              }
           }
        }
        
    2. Call the requestReadWritePermissions function in the onCreate function of your activity.

      @Override
      protected void onCreate(Bundle icicle) {
      
         // Request the permissions.
         requestReadWritePermissions();
      
  7. In Android Studio, set the build configuration to Profiling.

    This way you enable Kanzi Engine to collect profiling data at runtime.

    On Kanzi Android framework, Profiling Helper Tool by default outputs profiling results to the sdcard directory.

    _images/droidfw-change-config-to-profiling.png

See also

Getting started with Profiling Helper Tool