stat/stat_example.c

Reference Documentation

Platform
IntelĀ® PAC
Napatech SmartNIC
Content Type
Reference Information
Napatech Software Suite: stat/stat_example.c
stat/stat_example.c

Description

This source file is an example of how to use the statistics stream interface in NTAPI.

The following NTAPI functions are used:


Prerequisites

A working system is needed.

Program flow

The following is required to use the statistics stream interface in NTAPI:

  • #include/nt.h - Applications/Tools only need to include nt.h to obtain prototypes, macros etc. from NTAPI.
  • NT_Init(NTAPI_VERSION) - Initialize the NTAPI library. NTAPI_VERSION is a define that describes the version of the API described in the header files included by nt.h. NT_Init() will ask the NTAPI library to convert return data to the NTAPI_VERSION if possible. This will ensure that applications can run on NTAPI libraries of newer versions.
  • NT_StatOpen() - Open an statistics stream.
  • NT_StatRead() - Read statistics with clear=1 to clear the statistics counters for the current stream.
  • NT_StatRead() - Read statistics with clear=0 to obtain new statistics counters after the reset.
  • NT_StatClose() - Close the stream when terminating.
  • NT_Done() - Close down the NTAPI library.
  • NT_ExplainError() - Explain an error code returned by NTAPI functions.

/*
*
* Copyright 2020 Napatech A/S. All Rights Reserved.
*
* 1. Copying, modification, and distribution of this file, or executable
* versions of this file, is governed by the terms of the Napatech Software
* license agreement under which this file was made available. If you do not
* agree to the terms of the license do not install, copy, access or
* otherwise use this file.
*
* 2. Under the Napatech Software license agreement you are granted a
* limited, non-exclusive, non-assignable, copyright license to copy, modify
* and distribute this file in conjunction with Napatech SmartNIC's and
* similar hardware manufactured or supplied by Napatech A/S.
*
* 3. The full Napatech Software license agreement is included in this
* distribution, please see "NP-0405 Napatech Software license
* agreement.pdf"
*
* 4. Redistributions of source code must retain this copyright notice,
* list of conditions and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES, EXPRESS OR
* IMPLIED, AND NAPATECH DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING ANY
* IMPLIED WARRANTY OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, OR OF
* FITNESS FOR A PARTICULAR PURPOSE. TO THE EXTENT NOT PROHIBITED BY
* APPLICABLE LAW, IN NO EVENT SHALL NAPATECH BE LIABLE FOR PERSONAL INJURY,
* OR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES WHATSOEVER,
* INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, CORRUPTION OR
* LOSS OF DATA, FAILURE TO TRANSMIT OR RECEIVE ANY DATA OR INFORMATION,
* BUSINESS INTERRUPTION OR ANY OTHER COMMERCIAL DAMAGES OR LOSSES, ARISING
* OUT OF OR RELATED TO YOUR USE OR INABILITY TO USE NAPATECH SOFTWARE OR
* SERVICES OR ANY THIRD PARTY SOFTWARE OR APPLICATIONS IN CONJUNCTION WITH
* THE NAPATECH SOFTWARE OR SERVICES, HOWEVER CAUSED, REGARDLESS OF THE THEORY
* OF LIABILITY (CONTRACT, TORT OR OTHERWISE) AND EVEN IF NAPATECH HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME JURISDICTIONS DO NOT ALLOW
* THE EXCLUSION OR LIMITATION OF LIABILITY FOR PERSONAL INJURY, OR OF
* INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THIS LIMITATION MAY NOT APPLY TO YOU.
*
*
*/
/**
* @example stat/stat_example.c
* @section stat_example_description Description
*
* This source file is an example of how to use the @ref StatStream
* "statistics stream" interface in NTAPI.
*
* The following NTAPI functions are used:
* - @ref NT_Init()
* - @ref NT_StatOpen()
* - @ref NT_StatRead()
* - @ref NT_StatClose()
* - @ref NT_Done()
* - @ref NT_ExplainError()
*
* <hr>
* @section stat_example_prerequisites Prerequisites
* A working system is needed.
*
* @section stat_example_flow Program flow
* @{
* The following is required to use the @ref StatStream
* "statistics stream" interface in NTAPI:
* - \#include/nt.h - Applications/Tools only need to include @ref
* nt.h to obtain prototypes, macros etc. from NTAPI.
* - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI
* library. @ref NTAPI_VERSION is a define that describes the version
* of the API described in the header files included by @ref
* nt.h. NT_Init() will ask the NTAPI library to convert return data
* to the @ref NTAPI_VERSION if possible. This will ensure that
* applications can run on NTAPI libraries of newer versions.
* - @ref NT_StatOpen() - Open an statistics stream.
* - @ref NT_StatRead() - Read statistics with clear=1 to clear the statistics counters for the current stream.
* - @ref NT_StatRead() - Read statistics with clear=0 to obtain new statistics counters after the reset.
* - @ref NT_StatClose() - Close the stream when terminating.
* - @ref NT_Done() - Close down the NTAPI library.
* - @ref NT_ExplainError() - Explain an error code returned by NTAPI functions.
*
*<hr>
* @}
*/
#include <nt.h>
#if defined(__linux__) || defined(__FreeBSD__)
#include <unistd.h> // sleep()
#elif defined(WIN32) || defined (WIN64)
#include <winsock2.h> // Sleep()
#endif
int main(void)
{
NtStatStream_t hStatStream; // Statistics stream handle
NtStatistics_t hStat; // Stat handle.
char errorBuffer[NT_ERRBUF_SIZE]; // Error buffer
int status; // Status variable
unsigned i;
// Initialize the NTAPI library and thereby check if NTAPI_VERSION can be used together with this library
if((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
// Get the status code as text
NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
fprintf(stderr, "NT_Init() failed: %s\n", errorBuffer);
return -1;
}
// Open the stat stream
if((status = NT_StatOpen(&hStatStream, "ExampleStat")) != NT_SUCCESS) {
// Get the status code as text
NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
fprintf(stderr, "NT_StatOpen() failed: %s\n", errorBuffer);
return -1;
}
// Read the statistics counters to clear the statistics
// This is an optional step. If omitted, the adapter will show statistics form the start of ntservice.
hStat.u.query_v3.poll=0; // Wait for a new set
hStat.u.query_v3.clear=1; // Clear statistics
if((status = NT_StatRead(hStatStream, &hStat)) != NT_SUCCESS) {
// Get the status code as text
NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
fprintf(stderr, "NT_StatRead() failed: %s\n", errorBuffer);
return -1;
}
// Read new statistics for 10 seconds
printf("Statistics for port 0 the next 10 seconds.\n");
printf("--------------------------------------------------------------------------------\n");
for(i=0; i<10; i++) {
hStat.u.query_v3.poll=1; // The the current counters
hStat.u.query_v3.clear=0; // Do not clear statistics
if((status = NT_StatRead(hStatStream, &hStat)) != NT_SUCCESS) {
// Get the status code as text
NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
fprintf(stderr, "NT_StatRead() failed: %s\n", errorBuffer);
return -1;
}
// Print the RMON1 pkts and octets counters
printf("%2d: Port0 RX RMON1 pkts: %016llx, octets: %016llx\n",
i, (unsigned long long) hStat.u.query_v3.data.port.aPorts[0].rx.RMON1.pkts, (unsigned long long) hStat.u.query_v3.data.port.aPorts[0].rx.RMON1.octets);
} else {
printf("Port0 doesn't support RMON1 RX counters.\n");
}
printf(" Tx RMON1 pkts: %016llx, octets: %016llx\n",
(unsigned long long) hStat.u.query_v3.data.port.aPorts[0].tx.RMON1.pkts, (unsigned long long) hStat.u.query_v3.data.port.aPorts[0].tx.RMON1.octets);
} else {
printf("Port0 doesn't support RMON1 TX counters.\n");
}
printf("--------------------------------------------------------------------------------\n");
// Sleep 1 sec
#if defined(__linux__) || defined(__FreeBSD__)
sleep(1);
#elif defined(WIN32) || defined (WIN64)
Sleep(1000); // sleep 1000 milliseconds = 1 second
#endif
}
// Close the stat stream
if((status = NT_StatClose(hStatStream)) != NT_SUCCESS) {
// Get the status code as text
NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
fprintf(stderr, "NT_StatClose() failed: %s\n", errorBuffer);
return -1;
}
// Close down the NTAPI library
printf("Done.\n");
return 0;
}