PerfFPSMetrics

Demonstrates how to control FPS metrics when programming with ADLX.

Command Prompts

Command Prompt Description
1 Show current FPS metrics.
2 Show historical FPS metrics.
M/m Show this menu.
Q/q Quit.

Sample Path

/Samples/CPP/PerformanceMonitoring/PerfFPSMetrics

Code

Copied!

//
// Copyright (c) 2021 - 2025 Advanced Micro Devices, Inc. All rights reserved.
//
//-------------------------------------------------------------------------------------------------


#include "SDK/ADLXHelper/Windows/Cpp/ADLXHelper.h"
#include "SDK/Include/IPerformanceMonitoring.h"
#include <iostream>

// Use ADLX namespace
using namespace adlx;

// ADLXHelper instance
// No outstanding interfaces from ADLX must exist when ADLX is destroyed.
// Use global variables to ensure validity of the interface.
static ADLXHelper g_ADLXHelp;

// Main menu
void MainMenu();

// Menu action control
void MenuControl(IADLXPerformanceMonitoringServicesPtr perfMonitoringServices);

// Wait for exit with error message
int WaitAndExit(const char* msg, const int retCode);

/*
 * If the app only requires the current metric set, metrics tracking can be omitted in favor of calling the more efficient GetCurrent function.
 * If the app also requires metrics history, GetHistory retrieves the required accumulated history buffer, that may also include the current metric set.
 */
// Show current FPS metrics
void ShowCurrentFPSMetrics(IADLXPerformanceMonitoringServicesPtr perfMonitoringServices);

// Show historical FPS metrics
void ShowHistoricalFPSMetrics(IADLXPerformanceMonitoringServicesPtr perfMonitoringServices);

int main()
{
    ADLX_RESULT  res = ADLX_FAIL ;

    // Initialize ADLX
    res = g_ADLXHelp.Initialize();

    if (ADLX_SUCCEEDED (res))
    {
        // Get Performance Monitoring services
        IADLXPerformanceMonitoringServicesPtr perfMonitoringServices;
        ADLX_RESULT  res = g_ADLXHelp.GetSystemServices()->GetPerformanceMonitoringServices(&perfMonitoringServices);
        if (ADLX_SUCCEEDED (res))
        {
            // Display main menu options
            MainMenu();
            // Get and execute the choice
            MenuControl(perfMonitoringServices);
        }
        else
            std::cout << "\\tGet performance monitoring services failed" << std::endl;
    }
    else
        return WaitAndExit("\\tg_ADLXHelp initialize failed", 0);

    // Destroy ADLX
    res = g_ADLXHelp.Terminate();
    std::cout << "Destroy ADLX result: " << res << std::endl;

    // Pause to see the print out
    system("pause");

    return 0;
}

// Main menu
void MainMenu()
{
    std::cout << "\\tChoose one from the following options" << std::endl;

    std::cout << "\\t->Press 1 to display current FPS metrics" << std::endl;
    std::cout << "\\t->Press 2 to display historical FPS metrics" << std::endl;

    std::cout << "\\t->Press Q/q to terminate the application" << std::endl;
    std::cout << "\\t->Press M/m to display the main menu options" << std::endl;
}

// Menu action control
void MenuControl(IADLXPerformanceMonitoringServicesPtr perfMonitoringServices)
{
    int num = 0;
    while ((num = getchar()) != 'q' && num != 'Q')
    {
        switch (num)
        {
        // Show current metrics
        case '1':
            ShowCurrentFPSMetrics(perfMonitoringServices);
            break;

        // Show historical metrics
        case '2':
            ShowHistoricalFPSMetrics(perfMonitoringServices);
            break;

        // Display menu options
        case 'm':
        case 'M':
            MainMenu();
            break;
        default:
            break;
        }
    }
}

// Wait for exit with error message
int WaitAndExit(const char* msg, const int retCode)
{
    // Printout the message and pause to see it before returning the desired code
    if (nullptr != msg)
        std::cout << msg << std::endl;

    system("pause");
    return retCode;
}

// Display current FPS metrics
void ShowCurrentFPSMetrics(IADLXPerformanceMonitoringServicesPtr perfMonitoringServices)
{
    // Accumulate and display current metrics for each of 10 loops
    IADLXFPSPtr oneFPS;
    for (int i = 0; i < 10; ++i)
    {
        // Clear screen
        system("cls");
        // Get current FPS metrics
        ADLX_RESULT  res = perfMonitoringServices->GetCurrentFPS(&oneFPS);
        if (ADLX_SUCCEEDED (res))
        {
            adlx_int64 timeStamp = 0;
            res = oneFPS->TimeStamp(&timeStamp);
            if (ADLX_SUCCEEDED (res))
                std::cout << "The current metric time stamp is: " << timeStamp << "ms\\n";
            adlx_int fpsData = 0;
            res = oneFPS->FPS(&fpsData);
            if (ADLX_SUCCEEDED (res))
                std::cout << "The current metric FPS is: " << fpsData << std::endl;
            else if (res == ADLX_NOT_SUPPORTED )
                std::cout << "Don't support FPS" << std::endl;
        }
        Sleep(1000);
    }

    MainMenu();
}

// Show historical FPS metrics
void ShowHistoricalFPSMetrics(IADLXPerformanceMonitoringServicesPtr perfMonitoringServices)
{
    // Clear historical performance metrics data
    ADLX_RESULT  res = perfMonitoringServices->ClearPerformanceMetricsHistory();
    if (ADLX_FAILED (res))
    {
        std::cout << "Clear historical data failed" << std::endl;
        return;
    }

    // Start tracking performance metrics
    res = perfMonitoringServices->StartPerformanceMetricsTracking();
    if (ADLX_FAILED (res))
    {
        std::cout << "Start tracking performance metrics failed" << std::endl;
        return;
    }

    // Wait for 10 seconds to accumulate metrics
    std::cout << "Wait for 10 seconds to accumulate metrics..." << std::endl;
    Sleep(10000);

    // Get FPS metrics history from 10 seconds ago (10000 ms: the first parameter) to the present time(0 ms: the second parameter)
    IADLXFPSListPtr fpsList;
    res = perfMonitoringServices->GetFPSHistory(10000, 0, &fpsList);
    if (ADLX_SUCCEEDED (res))
    {
        // Display all listed FPS metrics
        IADLXFPSPtr oneFPS;
        for (int i = fpsList->Begin(); i != fpsList->End(); ++i)
        {
            std::cout << "********** historical FPS metrics "<< i + 1 << ": **********" << std::endl;
            res = fpsList->At(i, &oneFPS);
            if (ADLX_SUCCEEDED (res))
            {
                adlx_int64 timeStamp = 0;
                res = oneFPS->TimeStamp(&timeStamp);
                if (ADLX_SUCCEEDED (res))
                    std::cout << "The time stamp is: " << timeStamp << "ms" << std::endl;
                adlx_int fpsData = 0;
                res = oneFPS->FPS(&fpsData);
                if (ADLX_SUCCEEDED (res))
                    std::cout << "The FPS is: " << fpsData << std::endl;
                else if (res == ADLX_NOT_SUPPORTED )
                    std::cout << "Don't support FPS" << std::endl;
            }
            std::cout << std::endl;
        }
    }

    // Stop tracking performance metrics
    res = perfMonitoringServices->StopPerformanceMetricsTracking();
    if (ADLX_FAILED (res))
    {
        std::cout << "Stop tracking performance metrics failed" << std::endl;
        return;
    }
}

Related pages

  • Visit the Adlx product page for download links and more information.

Looking for more documentation on GPUOpen?

AMD GPUOpen software blogs

Our handy software release blogs will help you make good use of our tools, SDKs, and effects, as well as sharing the latest features with new releases.

GPUOpen Manuals

Don’t miss our manual documentation! And if slide decks are what you’re after, you’ll find 100+ of our finest presentations here.

AMD GPUOpen Performance Guides

The home of great performance and optimization advice for AMD RDNA™ 2 GPUs, AMD Ryzen™ CPUs, and so much more.

Getting started: AMD GPUOpen software

New or fairly new to AMD’s tools, libraries, and effects? This is the best place to get started on GPUOpen!

AMD GPUOpen Getting Started Development and Performance

Looking for tips on getting started with developing and/or optimizing your game, whether on AMD hardware or generally? We’ve got you covered!

AMD GPUOpen Technical blogs

Browse our technical blogs, and find valuable advice on developing with AMD hardware, ray tracing, Vulkan®, DirectX®, Unreal Engine, and lots more.

Find out more about our software!

AMD GPUOpen Effects - AMD FidelityFX technologies

Create wonder. No black boxes. Meet the AMD FidelityFX SDK!

AMD GPUOpen Samples

Browse all our useful samples. Perfect for when you’re needing to get started, want to integrate one of our libraries, and much more.

AMD GPUOpen developer SDKs

Discover what our SDK technologies can offer you. Query hardware or software, manage memory, create rendering applications or machine learning, and much more!

AMD GPUOpen Developer Tools

Analyze, Optimize, Profile, Benchmark. We provide you with the developer tools you need to make sure your game is the best it can be!