Open Ethernet Networking (OpEN) API Guide and Reference Manual  3.6.0.3
switch_cpu_example.c
/*********************************************************************
*
* Copyright 2018 Broadcom.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**********************************************************************
*
* @filename switch_cpu_example.c
*
* @purpose Switch CPU OpEN APIs Example
*
* @component OpEN
*
* @note
*
* @create 08/31/2018
*
* @end
*
**********************************************************************/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include "rpcclt_openapi.h"
#include "proc_util.h"
#include "openapi_common.h"
/***************************************************************/
void printAppMenu(char *name)
{
printf("Usage: %s <test#> <arg1> <arg2> ...\n", name);
printf("Test 1: Get max number of tracked periods: %s 1\n", name);
printf("Test 2: Get total CPU utilization for tracked periods: %s 2\n", name);
printf("Test 3: Run OpEN APIs Sanity: %s 3\n", name);
return;
}
/*****************************************************************/
open_error_t getMaxPeriods(openapiClientHandle_t *clientHandle, uint32_t *max)
{
open_error_t result;
if ((result = openapiCpuUtilMaxPeriodsGet(clientHandle, max)) != OPEN_E_NONE)
{
printf("Bad return code trying to get max periods. (result = %d)\n", result);
}
else
{
printf("Max periods is %d (result = %d)\n", *max, result);
}
return result;
}
/*****************************************************************/
void getTrackedUtilizationPeriods(openapiClientHandle_t *clientHandle)
{
open_error_t result;
uint32_t max;
open_buffdesc buffdesc;
OPEN_CPU_PERIOD_UTIL_INFO_t *periods = NULL;
int i;
result = getMaxPeriods(clientHandle, &max);
if (result == OPEN_E_NONE)
{
periods = (OPEN_CPU_PERIOD_UTIL_INFO_t *) malloc(max * sizeof(OPEN_CPU_PERIOD_UTIL_INFO_t));
if (periods == (OPEN_CPU_PERIOD_UTIL_INFO_t *) NULL)
{
printf("Unable to allocate array\n");
}
else
{
buffdesc.pstart = (void *) periods;
buffdesc.size = max * sizeof(OPEN_CPU_PERIOD_UTIL_INFO_t);
result = openapiTotalCpuUtilArrayGet(clientHandle, &buffdesc);
if (result != OPEN_E_NONE)
{
printf("Bad return code trying to get CPU utilization info. (result = %d)\n", result);
}
else
{
p = periods;
printf("Period Utilization\n");
printf("------ -----------\n");
for (i = 0; i < buffdesc.size / sizeof(OPEN_CPU_PERIOD_UTIL_INFO_t); i++)
{
printf("%6d %d.%-4d%%\n", p->timePeriod, p->utilization / OPEN_CPU_UTIL_SCALAR, p->utilization % OPEN_CPU_UTIL_SCALAR);
p++;
}
}
free(periods);
}
}
else
{
printf("Bad return code trying to get max number of tracked utilization periods. (result = %d)\n", result);
}
return;
}
/*****************************************************************/
void runSanityChecks(openapiClientHandle_t *clientHandle)
{
open_buffdesc buffdesc;
uint32_t max;
open_error_t result;
uint32_t constBufsize;
/* openapiCpuUtilMaxPeriodsGet */
printf("Testing openapiCpuUtilMaxPeriodsGet(): \n");
result = openapiCpuUtilMaxPeriodsGet(NULL, &max);
printf("NULL Client Handle. (result = %d)\n", result);
result = openapiCpuUtilMaxPeriodsGet(clientHandle, NULL);
printf("NULL argument. (result = %d)\n", result);
/* openapiTotalCpuUtilArrayGet */
printf("Testing openapiTotalCpuUtilArrayGet(): \n");
openapiCpuUtilMaxPeriodsGet(clientHandle, &max);
constBufsize = max * sizeof(OPEN_CPU_PERIOD_UTIL_INFO_t); /* constant for remainder of this function */
p = (OPEN_CPU_PERIOD_UTIL_INFO_t *) malloc(constBufsize);
buffdesc.pstart = (void *) p;
buffdesc.size = constBufsize;
result = openapiTotalCpuUtilArrayGet(NULL, &buffdesc);
printf("NULL Client Handle. (result = %d)\n", result);
buffdesc.pstart = (void *) NULL;
buffdesc.size = constBufsize;
result = openapiTotalCpuUtilArrayGet(clientHandle, &buffdesc);
printf("NULL buffdesc pstart. (result = %d)\n", result);
buffdesc.pstart = (void *) p;
buffdesc.size = constBufsize - 1;
result = openapiTotalCpuUtilArrayGet(clientHandle, &buffdesc);
printf("NULL buffdesc invalid size. (result = %d)\n", result);
free(p);
}
/***************************************************************/
int main(int argc, char **argv)
{
openapiClientHandle_t clientHandle;
open_error_t result;
uint32_t testNum;
uint32_t max;
open_buffdesc switch_os_revision;
open_buffdesc switch_hw_version;
open_revision_data_t openApiVersion;
char switch_os_revision_string[OPENAPI_OS_REVISION_STR_SIZE];
char switch_hw_version_string[OPENAPI_HW_VERSION_STR_SIZE];
if (argc < 2)
{
printAppMenu(argv[0]);
exit(1);
}
testNum = atoi(argv[1]);
l7proc_crashlog_register();
/* Register with OpEN */
if ((result = openapiClientRegister(argv[0], &clientHandle)) != OPEN_E_NONE)
{
printf("\nFailed to initialize RPC to OpEN. Exiting (result = %d)\n", result);
exit(2);
}
/* RPC call can fail until server starts. Keep trying */
while (openapiConnectivityCheck(&clientHandle) != OPEN_E_NONE)
{
sleep(1);
}
L7PROC_LOGF(L7PROC_LOG_SEVERITY_INFO, 0, "Starting CPU utilization tracking example application");
printf("\n");
switch_os_revision.pstart = switch_os_revision_string;
switch_os_revision.size = sizeof(switch_os_revision_string);
switch_hw_version.pstart = switch_hw_version_string;
switch_hw_version.size = sizeof(switch_hw_version_string);
if (openapiNetworkOSVersionGet(&clientHandle, &switch_os_revision) == OPEN_E_NONE)
printf("Network OS Version = %s\n", switch_os_revision_string);
else
printf("Network OS Version retrieve error\n");
if (openapiApiVersionGet(&clientHandle, &openApiVersion) == OPEN_E_NONE)
printf("Open API Version = %d.%d.%d.%d\n",
openApiVersion.release,
openApiVersion.version,
openApiVersion.maint_level,
openApiVersion.build_num);
else
printf("Open API Version retrieve error\n");
if (openapiHardwareVersionGet(&clientHandle, &switch_hw_version) == OPEN_E_NONE)
printf("Network Processing Device = %s\n", switch_hw_version_string);
else
printf("Network processing device retrieve error\n");
printf("\n");
switch (testNum)
{
case 1:
if (argc != 2)
{
printAppMenu(argv[0]);
exit(1);
}
getMaxPeriods(&clientHandle, &max);
break;
case 2:
if (argc != 2)
{
printAppMenu(argv[0]);
exit(1);
}
getTrackedUtilizationPeriods(&clientHandle);
break;
case 3:
if (argc != 2)
{
printAppMenu(argv[0]);
exit(1);
}
runSanityChecks(&clientHandle);
break;
default:
printAppMenu(argv[0]);
break;
}
/* Log goodbye message with OpEN */
L7PROC_LOGF(L7PROC_LOG_SEVERITY_INFO, 0, "Stopping CPU utilization tracking example application");
(void) openapiClientTearDown(&clientHandle);
return 0;
}