LCOV - code coverage report
Current view: top level - app/test - test_power_cpufreq.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 7 263 2.7 %
Date: 2024-01-22 15:35:40 Functions: 3 13 23.1 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 174 0.6 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdio.h>
       6                 :            : #include <stdint.h>
       7                 :            : #include <unistd.h>
       8                 :            : #include <limits.h>
       9                 :            : #include <string.h>
      10                 :            : #include <inttypes.h>
      11                 :            : #include <rte_cycles.h>
      12                 :            : 
      13                 :            : #include "test.h"
      14                 :            : 
      15                 :            : #ifndef RTE_LIB_POWER
      16                 :            : 
      17                 :            : static int
      18                 :            : test_power_cpufreq(void)
      19                 :            : {
      20                 :            :         printf("Power management library not supported, skipping test\n");
      21                 :            :         return TEST_SKIPPED;
      22                 :            : }
      23                 :            : 
      24                 :            : static int
      25                 :            : test_power_caps(void)
      26                 :            : {
      27                 :            :         printf("Power management library not supported, skipping test\n");
      28                 :            :         return TEST_SKIPPED;
      29                 :            : }
      30                 :            : 
      31                 :            : #else
      32                 :            : #include <rte_power.h>
      33                 :            : 
      34                 :            : #define TEST_POWER_LCORE_ID      2U
      35                 :            : #define TEST_POWER_LCORE_INVALID ((unsigned)RTE_MAX_LCORE)
      36                 :            : #define TEST_POWER_FREQS_NUM_MAX ((unsigned)RTE_MAX_LCORE_FREQS)
      37                 :            : 
      38                 :            : /* macros used for rounding frequency to nearest 100000 */
      39                 :            : #define TEST_FREQ_ROUNDING_DELTA 50000
      40                 :            : #define TEST_ROUND_FREQ_TO_N_100000 100000
      41                 :            : 
      42                 :            : #define TEST_POWER_SYSFILE_CPUINFO_FREQ \
      43                 :            :         "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_cur_freq"
      44                 :            : #define TEST_POWER_SYSFILE_SCALING_FREQ \
      45                 :            :         "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
      46                 :            : 
      47                 :            : static uint32_t total_freq_num;
      48                 :            : static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
      49                 :            : 
      50                 :            : static int
      51         [ #  # ]:          0 : check_cur_freq(unsigned int lcore_id, uint32_t idx, bool turbo)
      52                 :            : {
      53                 :            : #define TEST_POWER_CONVERT_TO_DECIMAL 10
      54                 :            : #define MAX_LOOP 100
      55                 :            :         FILE *f;
      56                 :            :         char fullpath[PATH_MAX];
      57                 :            :         char buf[BUFSIZ];
      58                 :            :         enum power_management_env env;
      59                 :            :         uint32_t cur_freq;
      60                 :            :         uint32_t freq_conv;
      61                 :            :         int ret = -1;
      62                 :            :         int i;
      63                 :            : 
      64         [ #  # ]:          0 :         if (snprintf(fullpath, sizeof(fullpath),
      65                 :            :                 TEST_POWER_SYSFILE_CPUINFO_FREQ, lcore_id) < 0) {
      66                 :            :                 return 0;
      67                 :            :         }
      68                 :          0 :         f = fopen(fullpath, "r");
      69         [ #  # ]:          0 :         if (f == NULL) {
      70         [ #  # ]:          0 :                 if (snprintf(fullpath, sizeof(fullpath),
      71                 :            :                         TEST_POWER_SYSFILE_SCALING_FREQ, lcore_id) < 0) {
      72                 :            :                         return 0;
      73                 :            :                 }
      74                 :          0 :                 f = fopen(fullpath, "r");
      75         [ #  # ]:          0 :                 if (f == NULL) {
      76                 :            :                         return 0;
      77                 :            :                 }
      78                 :            :         }
      79         [ #  # ]:          0 :         for (i = 0; i < MAX_LOOP; i++) {
      80                 :          0 :                 fflush(f);
      81         [ #  # ]:          0 :                 if (fgets(buf, sizeof(buf), f) == NULL)
      82                 :          0 :                         goto fail_all;
      83                 :            : 
      84                 :          0 :                 cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
      85                 :            :                 freq_conv = cur_freq;
      86                 :            : 
      87                 :          0 :                 env = rte_power_get_env();
      88         [ #  # ]:          0 :                 if (env == PM_ENV_CPPC_CPUFREQ || env == PM_ENV_PSTATE_CPUFREQ) {
      89                 :            :                         /* convert the frequency to nearest 100000 value
      90                 :            :                          * Ex: if cur_freq=1396789 then freq_conv=1400000
      91                 :            :                          * Ex: if cur_freq=800030 then freq_conv=800000
      92                 :            :                          */
      93                 :          0 :                         freq_conv = (cur_freq + TEST_FREQ_ROUNDING_DELTA)
      94                 :            :                                                 / TEST_ROUND_FREQ_TO_N_100000;
      95                 :          0 :                         freq_conv = freq_conv * TEST_ROUND_FREQ_TO_N_100000;
      96         [ #  # ]:          0 :                 } else if (env == PM_ENV_AMD_PSTATE_CPUFREQ) {
      97         [ #  # ]:          0 :                         freq_conv = cur_freq > freqs[idx] ? (cur_freq - freqs[idx]) :
      98                 :            :                                                         (freqs[idx] - cur_freq);
      99         [ #  # ]:          0 :                         if (freq_conv <= TEST_FREQ_ROUNDING_DELTA) {
     100                 :            :                                 /* workaround: current frequency may deviate from
     101                 :            :                                  * nominal freq. Allow deviation of up to 50Mhz.
     102                 :            :                                  */
     103                 :            :                                 printf("Current frequency deviated from nominal "
     104                 :            :                                         "frequency by %d Khz!\n", freq_conv);
     105                 :          0 :                                 freq_conv = freqs[idx];
     106                 :            :                         }
     107                 :            :                 }
     108                 :            : 
     109         [ #  # ]:          0 :                 if (turbo)
     110         [ #  # ]:          0 :                         ret = (freqs[idx] <= freq_conv ? 0 : -1);
     111                 :            :                 else
     112         [ #  # ]:          0 :                         ret = (freqs[idx] == freq_conv ? 0 : -1);
     113                 :            : 
     114                 :            :                 if (ret == 0)
     115                 :            :                         break;
     116                 :            : 
     117         [ #  # ]:          0 :                 if (fseek(f, 0, SEEK_SET) < 0) {
     118                 :            :                         printf("Fail to set file position indicator to 0\n");
     119                 :          0 :                         goto fail_all;
     120                 :            :                 }
     121                 :            : 
     122                 :            :                 /* wait for the value to be updated */
     123                 :            :                 rte_delay_ms(10);
     124                 :            :         }
     125                 :            : 
     126                 :          0 : fail_all:
     127                 :          0 :         fclose(f);
     128                 :            : 
     129                 :          0 :         return ret;
     130                 :            : }
     131                 :            : 
     132                 :            : /* Check rte_power_freqs() */
     133                 :            : static int
     134                 :          0 : check_power_freqs(void)
     135                 :            : {
     136                 :            :         uint32_t ret;
     137                 :            : 
     138                 :          0 :         total_freq_num = 0;
     139                 :            :         memset(freqs, 0, sizeof(freqs));
     140                 :            : 
     141                 :            :         /* test with an invalid lcore id */
     142                 :          0 :         ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
     143                 :            :                                         TEST_POWER_FREQS_NUM_MAX);
     144         [ #  # ]:          0 :         if (ret > 0) {
     145                 :            :                 printf("Unexpectedly get available freqs successfully on "
     146                 :            :                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
     147                 :          0 :                 return -1;
     148                 :            :         }
     149                 :            : 
     150                 :            :         /* test with NULL buffer to save available freqs */
     151                 :          0 :         ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
     152                 :            :                                 TEST_POWER_FREQS_NUM_MAX);
     153         [ #  # ]:          0 :         if (ret > 0) {
     154                 :            :                 printf("Unexpectedly get available freqs successfully with "
     155                 :            :                         "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
     156                 :          0 :                 return -1;
     157                 :            :         }
     158                 :            : 
     159                 :            :         /* test of getting zero number of freqs */
     160                 :          0 :         ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
     161         [ #  # ]:          0 :         if (ret > 0) {
     162                 :            :                 printf("Unexpectedly get available freqs successfully with "
     163                 :            :                         "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
     164                 :          0 :                 return -1;
     165                 :            :         }
     166                 :            : 
     167                 :            :         /* test with all valid input parameters */
     168                 :          0 :         ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
     169                 :            :                                 TEST_POWER_FREQS_NUM_MAX);
     170         [ #  # ]:          0 :         if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
     171                 :            :                 printf("Fail to get available freqs on lcore %u\n",
     172                 :            :                                                 TEST_POWER_LCORE_ID);
     173                 :          0 :                 return -1;
     174                 :            :         }
     175                 :            : 
     176                 :            :         /* Save the total number of available freqs */
     177                 :          0 :         total_freq_num = ret;
     178                 :            : 
     179                 :          0 :         return 0;
     180                 :            : }
     181                 :            : 
     182                 :            : /* Check rte_power_get_freq() */
     183                 :            : static int
     184                 :          0 : check_power_get_freq(void)
     185                 :            : {
     186                 :            :         int ret;
     187                 :            :         uint32_t count;
     188                 :            : 
     189                 :            :         /* test with an invalid lcore id */
     190                 :          0 :         count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
     191         [ #  # ]:          0 :         if (count < TEST_POWER_FREQS_NUM_MAX) {
     192                 :            :                 printf("Unexpectedly get freq index successfully on "
     193                 :            :                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
     194                 :          0 :                 return -1;
     195                 :            :         }
     196                 :            : 
     197                 :          0 :         count = rte_power_get_freq(TEST_POWER_LCORE_ID);
     198         [ #  # ]:          0 :         if (count >= TEST_POWER_FREQS_NUM_MAX) {
     199                 :            :                 printf("Fail to get the freq index on lcore %u\n",
     200                 :            :                                                 TEST_POWER_LCORE_ID);
     201                 :          0 :                 return -1;
     202                 :            :         }
     203                 :            : 
     204                 :            :         /* Check the current frequency */
     205                 :          0 :         ret = check_cur_freq(TEST_POWER_LCORE_ID, count, false);
     206         [ #  # ]:          0 :         if (ret < 0)
     207                 :          0 :                 return -1;
     208                 :            : 
     209                 :            :         return 0;
     210                 :            : }
     211                 :            : 
     212                 :            : /* Check rte_power_set_freq() */
     213                 :            : static int
     214                 :          0 : check_power_set_freq(void)
     215                 :            : {
     216                 :            :         int ret;
     217                 :            : 
     218                 :            :         /* test with an invalid lcore id */
     219                 :          0 :         ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
     220         [ #  # ]:          0 :         if (ret >= 0) {
     221                 :            :                 printf("Unexpectedly set freq index successfully on "
     222                 :            :                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
     223                 :          0 :                 return -1;
     224                 :            :         }
     225                 :            : 
     226                 :            :         /* test with an invalid freq index */
     227                 :          0 :         ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
     228                 :            :                                 TEST_POWER_FREQS_NUM_MAX);
     229         [ #  # ]:          0 :         if (ret >= 0) {
     230                 :            :                 printf("Unexpectedly set an invalid freq index (%u)"
     231                 :            :                         "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
     232                 :            :                                                         TEST_POWER_LCORE_ID);
     233                 :          0 :                 return -1;
     234                 :            :         }
     235                 :            : 
     236                 :            :         /**
     237                 :            :          * test with an invalid freq index which is right one bigger than
     238                 :            :          * total number of freqs
     239                 :            :          */
     240                 :          0 :         ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
     241         [ #  # ]:          0 :         if (ret >= 0) {
     242                 :          0 :                 printf("Unexpectedly set an invalid freq index (%u)"
     243                 :            :                         "successfully on lcore %u\n", total_freq_num,
     244                 :            :                                                 TEST_POWER_LCORE_ID);
     245                 :          0 :                 return -1;
     246                 :            :         }
     247                 :          0 :         ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
     248         [ #  # ]:          0 :         if (ret < 0) {
     249                 :            :                 printf("Fail to set freq index on lcore %u\n",
     250                 :            :                                         TEST_POWER_LCORE_ID);
     251                 :          0 :                 return -1;
     252                 :            :         }
     253                 :            : 
     254                 :            :         /* Check the current frequency */
     255                 :          0 :         ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1, false);
     256         [ #  # ]:          0 :         if (ret < 0)
     257                 :          0 :                 return -1;
     258                 :            : 
     259                 :            :         return 0;
     260                 :            : }
     261                 :            : 
     262                 :            : /* Check rte_power_freq_down() */
     263                 :            : static int
     264                 :          0 : check_power_freq_down(void)
     265                 :            : {
     266                 :            :         int ret;
     267                 :            : 
     268                 :          0 :         rte_power_freq_enable_turbo(TEST_POWER_LCORE_ID);
     269                 :            : 
     270                 :            :         /* test with an invalid lcore id */
     271                 :          0 :         ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
     272         [ #  # ]:          0 :         if (ret >= 0) {
     273                 :            :                 printf("Unexpectedly scale down successfully the freq on "
     274                 :            :                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
     275                 :          0 :                 return -1;
     276                 :            :         }
     277                 :            : 
     278                 :            :         /* Scale down to min and then scale down one step */
     279                 :          0 :         ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
     280         [ #  # ]:          0 :         if (ret < 0) {
     281                 :            :                 printf("Fail to scale down the freq to min on lcore %u\n",
     282                 :            :                                                         TEST_POWER_LCORE_ID);
     283                 :          0 :                 return -1;
     284                 :            :         }
     285                 :          0 :         ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
     286         [ #  # ]:          0 :         if (ret < 0) {
     287                 :            :                 printf("Fail to scale down the freq on lcore %u\n",
     288                 :            :                                                 TEST_POWER_LCORE_ID);
     289                 :          0 :                 return -1;
     290                 :            :         }
     291                 :            : 
     292                 :            :         /* Check the current frequency */
     293                 :          0 :         ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1, false);
     294         [ #  # ]:          0 :         if (ret < 0)
     295                 :            :                 return -1;
     296                 :            : 
     297                 :            :         /* Scale up to max and then scale down one step */
     298                 :          0 :         ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
     299         [ #  # ]:          0 :         if (ret < 0) {
     300                 :            :                 printf("Fail to scale up the freq to max on lcore %u\n",
     301                 :            :                                                         TEST_POWER_LCORE_ID);
     302                 :          0 :                 return -1;
     303                 :            :         }
     304                 :          0 :         ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
     305         [ #  # ]:          0 :         if (ret < 0) {
     306                 :            :                 printf("Fail to scale down the freq on lcore %u\n",
     307                 :            :                                                 TEST_POWER_LCORE_ID);
     308                 :          0 :                 return -1;
     309                 :            :         }
     310                 :            : 
     311                 :            :         /* Check the current frequency */
     312                 :          0 :         ret = check_cur_freq(TEST_POWER_LCORE_ID, 1, false);
     313         [ #  # ]:          0 :         if (ret < 0)
     314                 :          0 :                 return -1;
     315                 :            : 
     316                 :            :         return 0;
     317                 :            : }
     318                 :            : 
     319                 :            : /* Check rte_power_freq_up() */
     320                 :            : static int
     321                 :          0 : check_power_freq_up(void)
     322                 :            : {
     323                 :            :         int ret;
     324                 :            : 
     325                 :            :         /* test with an invalid lcore id */
     326                 :          0 :         ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
     327         [ #  # ]:          0 :         if (ret >= 0) {
     328                 :            :                 printf("Unexpectedly scale up successfully the freq on %u\n",
     329                 :            :                                                 TEST_POWER_LCORE_INVALID);
     330                 :          0 :                 return -1;
     331                 :            :         }
     332                 :            : 
     333                 :            :         /* Scale down to min and then scale up one step */
     334                 :          0 :         ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
     335         [ #  # ]:          0 :         if (ret < 0) {
     336                 :            :                 printf("Fail to scale down the freq to min on lcore %u\n",
     337                 :            :                                                         TEST_POWER_LCORE_ID);
     338                 :          0 :                 return -1;
     339                 :            :         }
     340                 :          0 :         ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
     341         [ #  # ]:          0 :         if (ret < 0) {
     342                 :            :                 printf("Fail to scale up the freq on lcore %u\n",
     343                 :            :                                                 TEST_POWER_LCORE_ID);
     344                 :          0 :                 return -1;
     345                 :            :         }
     346                 :            : 
     347                 :            :         /* Check the current frequency */
     348                 :          0 :         ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2, false);
     349         [ #  # ]:          0 :         if (ret < 0)
     350                 :            :                 return -1;
     351                 :            : 
     352                 :            :         /* Scale up to max and then scale up one step */
     353                 :          0 :         ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
     354         [ #  # ]:          0 :         if (ret < 0) {
     355                 :            :                 printf("Fail to scale up the freq to max on lcore %u\n",
     356                 :            :                                                 TEST_POWER_LCORE_ID);
     357                 :          0 :                 return -1;
     358                 :            :         }
     359                 :          0 :         ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
     360         [ #  # ]:          0 :         if (ret < 0) {
     361                 :            :                 printf("Fail to scale up the freq on lcore %u\n",
     362                 :            :                                                 TEST_POWER_LCORE_ID);
     363                 :          0 :                 return -1;
     364                 :            :         }
     365                 :            : 
     366                 :            :         /* Check the current frequency */
     367                 :          0 :         ret = check_cur_freq(TEST_POWER_LCORE_ID, 0, true);
     368         [ #  # ]:          0 :         if (ret < 0)
     369                 :          0 :                 return -1;
     370                 :            : 
     371                 :            :         return 0;
     372                 :            : }
     373                 :            : 
     374                 :            : /* Check rte_power_freq_max() */
     375                 :            : static int
     376                 :          0 : check_power_freq_max(void)
     377                 :            : {
     378                 :            :         int ret;
     379                 :            : 
     380                 :            :         /* test with an invalid lcore id */
     381                 :          0 :         ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
     382         [ #  # ]:          0 :         if (ret >= 0) {
     383                 :            :                 printf("Unexpectedly scale up successfully the freq to max on "
     384                 :            :                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
     385                 :          0 :                 return -1;
     386                 :            :         }
     387                 :          0 :         ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
     388         [ #  # ]:          0 :         if (ret < 0) {
     389                 :            :                 printf("Fail to scale up the freq to max on lcore %u\n",
     390                 :            :                                                 TEST_POWER_LCORE_ID);
     391                 :          0 :                 return -1;
     392                 :            :         }
     393                 :            : 
     394                 :            :         /* Check the current frequency */
     395                 :          0 :         ret = check_cur_freq(TEST_POWER_LCORE_ID, 0, true);
     396         [ #  # ]:          0 :         if (ret < 0)
     397                 :          0 :                 return -1;
     398                 :            : 
     399                 :            :         return 0;
     400                 :            : }
     401                 :            : 
     402                 :            : /* Check rte_power_freq_min() */
     403                 :            : static int
     404                 :          0 : check_power_freq_min(void)
     405                 :            : {
     406                 :            :         int ret;
     407                 :            : 
     408                 :            :         /* test with an invalid lcore id */
     409                 :          0 :         ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
     410         [ #  # ]:          0 :         if (ret >= 0) {
     411                 :            :                 printf("Unexpectedly scale down successfully the freq to min "
     412                 :            :                                 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
     413                 :          0 :                 return -1;
     414                 :            :         }
     415                 :          0 :         ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
     416         [ #  # ]:          0 :         if (ret < 0) {
     417                 :            :                 printf("Fail to scale down the freq to min on lcore %u\n",
     418                 :            :                                                         TEST_POWER_LCORE_ID);
     419                 :          0 :                 return -1;
     420                 :            :         }
     421                 :            : 
     422                 :            :         /* Check the current frequency */
     423                 :          0 :         ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1, false);
     424         [ #  # ]:          0 :         if (ret < 0)
     425                 :          0 :                 return -1;
     426                 :            : 
     427                 :            :         return 0;
     428                 :            : }
     429                 :            : 
     430                 :            : /* Check rte_power_turbo() */
     431                 :            : static int
     432                 :          0 : check_power_turbo(void)
     433                 :            : {
     434                 :            :         int ret;
     435                 :            : 
     436         [ #  # ]:          0 :         if (rte_power_turbo_status(TEST_POWER_LCORE_ID) == 0) {
     437                 :            :                 printf("Turbo not available on lcore %u, skipping test\n",
     438                 :            :                                 TEST_POWER_LCORE_ID);
     439                 :          0 :                 return 0;
     440                 :            :         }
     441                 :            : 
     442                 :            :         /* test with an invalid lcore id */
     443                 :          0 :         ret = rte_power_freq_enable_turbo(TEST_POWER_LCORE_INVALID);
     444         [ #  # ]:          0 :         if (ret >= 0) {
     445                 :            :                 printf("Unexpectedly enable turbo successfully on lcore %u\n",
     446                 :            :                                 TEST_POWER_LCORE_INVALID);
     447                 :          0 :                 return -1;
     448                 :            :         }
     449                 :          0 :         ret = rte_power_freq_enable_turbo(TEST_POWER_LCORE_ID);
     450         [ #  # ]:          0 :         if (ret < 0) {
     451                 :            :                 printf("Fail to enable turbo on lcore %u\n",
     452                 :            :                                 TEST_POWER_LCORE_ID);
     453                 :          0 :                 return -1;
     454                 :            :         }
     455                 :          0 :         ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
     456         [ #  # ]:          0 :         if (ret < 0) {
     457                 :            :                 printf("Fail to scale up the freq to max on lcore %u\n",
     458                 :            :                                                 TEST_POWER_LCORE_ID);
     459                 :          0 :                 return -1;
     460                 :            :         }
     461                 :            : 
     462                 :            :         /* Check the current frequency */
     463                 :          0 :         ret = check_cur_freq(TEST_POWER_LCORE_ID, 0, true);
     464         [ #  # ]:          0 :         if (ret < 0)
     465                 :            :                 return -1;
     466                 :            : 
     467                 :            :         /* test with an invalid lcore id */
     468                 :          0 :         ret = rte_power_freq_disable_turbo(TEST_POWER_LCORE_INVALID);
     469         [ #  # ]:          0 :         if (ret >= 0) {
     470                 :            :                 printf("Unexpectedly disable turbo successfully on lcore %u\n",
     471                 :            :                                 TEST_POWER_LCORE_INVALID);
     472                 :          0 :                 return -1;
     473                 :            :         }
     474                 :          0 :         ret = rte_power_freq_disable_turbo(TEST_POWER_LCORE_ID);
     475         [ #  # ]:          0 :         if (ret < 0) {
     476                 :            :                 printf("Fail to disable turbo on lcore %u\n",
     477                 :            :                                 TEST_POWER_LCORE_ID);
     478                 :          0 :                 return -1;
     479                 :            :         }
     480                 :          0 :         ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
     481         [ #  # ]:          0 :         if (ret < 0) {
     482                 :            :                 printf("Fail to scale up the freq to max on lcore %u\n",
     483                 :            :                                                 TEST_POWER_LCORE_ID);
     484                 :          0 :                 return -1;
     485                 :            :         }
     486                 :            : 
     487                 :            :         /* Check the current frequency */
     488                 :          0 :         ret = check_cur_freq(TEST_POWER_LCORE_ID, 1, false);
     489         [ #  # ]:          0 :         if (ret < 0)
     490                 :          0 :                 return -1;
     491                 :            : 
     492                 :            :         return 0;
     493                 :            : }
     494                 :            : 
     495                 :            : static int
     496                 :          1 : test_power_cpufreq(void)
     497                 :            : {
     498                 :            :         int ret = -1;
     499                 :            :         enum power_management_env env;
     500                 :            : 
     501                 :            :         /* Test initialisation of a valid lcore */
     502                 :          1 :         ret = rte_power_init(TEST_POWER_LCORE_ID);
     503         [ +  - ]:          1 :         if (ret < 0) {
     504                 :            :                 printf("Cannot initialise power management for lcore %u, this "
     505                 :            :                                 "may occur if environment is not configured "
     506                 :            :                                 "correctly(APCI cpufreq) or operating in another valid "
     507                 :            :                                 "Power management environment\n",
     508                 :            :                                 TEST_POWER_LCORE_ID);
     509                 :          1 :                 rte_power_unset_env();
     510                 :          1 :                 return TEST_SKIPPED;
     511                 :            :         }
     512                 :            : 
     513                 :            :         /* Test environment configuration */
     514                 :          0 :         env = rte_power_get_env();
     515         [ #  # ]:          0 :         if ((env != PM_ENV_ACPI_CPUFREQ) && (env != PM_ENV_PSTATE_CPUFREQ) &&
     516         [ #  # ]:          0 :                         (env != PM_ENV_CPPC_CPUFREQ) &&
     517                 :            :                         (env != PM_ENV_AMD_PSTATE_CPUFREQ)) {
     518                 :            :                 printf("Unexpectedly got an environment other than ACPI/PSTATE\n");
     519                 :          0 :                 goto fail_all;
     520                 :            :         }
     521                 :            : 
     522                 :            :         /* verify that function pointers are not NULL */
     523         [ #  # ]:          0 :         if (rte_power_freqs == NULL) {
     524                 :            :                 printf("rte_power_freqs should not be NULL, environment has not been "
     525                 :            :                                 "initialised\n");
     526                 :          0 :                 goto fail_all;
     527                 :            :         }
     528         [ #  # ]:          0 :         if (rte_power_get_freq == NULL) {
     529                 :            :                 printf("rte_power_get_freq should not be NULL, environment has not "
     530                 :            :                                 "been initialised\n");
     531                 :          0 :                 goto fail_all;
     532                 :            :         }
     533         [ #  # ]:          0 :         if (rte_power_set_freq == NULL) {
     534                 :            :                 printf("rte_power_set_freq should not be NULL, environment has not "
     535                 :            :                                 "been initialised\n");
     536                 :          0 :                 goto fail_all;
     537                 :            :         }
     538         [ #  # ]:          0 :         if (rte_power_freq_up == NULL) {
     539                 :            :                 printf("rte_power_freq_up should not be NULL, environment has not "
     540                 :            :                                 "been initialised\n");
     541                 :          0 :                 goto fail_all;
     542                 :            :         }
     543         [ #  # ]:          0 :         if (rte_power_freq_down == NULL) {
     544                 :            :                 printf("rte_power_freq_down should not be NULL, environment has not "
     545                 :            :                                 "been initialised\n");
     546                 :          0 :                 goto fail_all;
     547                 :            :         }
     548         [ #  # ]:          0 :         if (rte_power_freq_max == NULL) {
     549                 :            :                 printf("rte_power_freq_max should not be NULL, environment has not "
     550                 :            :                                 "been initialised\n");
     551                 :          0 :                 goto fail_all;
     552                 :            :         }
     553         [ #  # ]:          0 :         if (rte_power_freq_min == NULL) {
     554                 :            :                 printf("rte_power_freq_min should not be NULL, environment has not "
     555                 :            :                                 "been initialised\n");
     556                 :          0 :                 goto fail_all;
     557                 :            :         }
     558         [ #  # ]:          0 :         if (rte_power_turbo_status == NULL) {
     559                 :            :                 printf("rte_power_turbo_status should not be NULL, environment has not "
     560                 :            :                                 "been initialised\n");
     561                 :          0 :                 goto fail_all;
     562                 :            :         }
     563         [ #  # ]:          0 :         if (rte_power_freq_enable_turbo == NULL) {
     564                 :            :                 printf("rte_power_freq_enable_turbo should not be NULL, environment has not "
     565                 :            :                                 "been initialised\n");
     566                 :          0 :                 goto fail_all;
     567                 :            :         }
     568         [ #  # ]:          0 :         if (rte_power_freq_disable_turbo == NULL) {
     569                 :            :                 printf("rte_power_freq_disable_turbo should not be NULL, environment has not "
     570                 :            :                                 "been initialised\n");
     571                 :          0 :                 goto fail_all;
     572                 :            :         }
     573                 :            : 
     574                 :          0 :         ret = rte_power_exit(TEST_POWER_LCORE_ID);
     575         [ #  # ]:          0 :         if (ret < 0) {
     576                 :            :                 printf("Cannot exit power management for lcore %u\n",
     577                 :            :                                                 TEST_POWER_LCORE_ID);
     578                 :          0 :                 rte_power_unset_env();
     579                 :          0 :                 return -1;
     580                 :            :         }
     581                 :            : 
     582                 :            :         /* test of init power management for an invalid lcore */
     583                 :          0 :         ret = rte_power_init(TEST_POWER_LCORE_INVALID);
     584         [ #  # ]:          0 :         if (ret == 0) {
     585                 :            :                 printf("Unexpectedly initialise power management successfully "
     586                 :            :                                 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
     587                 :          0 :                 rte_power_unset_env();
     588                 :          0 :                 return -1;
     589                 :            :         }
     590                 :            : 
     591                 :            :         /* Test initialisation of a valid lcore */
     592                 :          0 :         ret = rte_power_init(TEST_POWER_LCORE_ID);
     593         [ #  # ]:          0 :         if (ret < 0) {
     594                 :            :                 printf("Cannot initialise power management for lcore %u, this "
     595                 :            :                                 "may occur if environment is not configured "
     596                 :            :                                 "correctly(APCI cpufreq) or operating in another valid "
     597                 :            :                                 "Power management environment\n", TEST_POWER_LCORE_ID);
     598                 :          0 :                 rte_power_unset_env();
     599                 :          0 :                 return TEST_SKIPPED;
     600                 :            :         }
     601                 :            : 
     602                 :            :         /**
     603                 :            :          * test of initialising power management for the lcore which has
     604                 :            :          * been initialised
     605                 :            :          */
     606                 :          0 :         ret = rte_power_init(TEST_POWER_LCORE_ID);
     607         [ #  # ]:          0 :         if (ret == 0) {
     608                 :            :                 printf("Unexpectedly init successfully power twice on "
     609                 :            :                                         "lcore %u\n", TEST_POWER_LCORE_ID);
     610                 :          0 :                 goto fail_all;
     611                 :            :         }
     612                 :            : 
     613                 :          0 :         ret = check_power_freqs();
     614         [ #  # ]:          0 :         if (ret < 0)
     615                 :          0 :                 goto fail_all;
     616                 :            : 
     617         [ #  # ]:          0 :         if (total_freq_num < 2) {
     618                 :          0 :                 rte_power_exit(TEST_POWER_LCORE_ID);
     619                 :            :                 printf("Frequency can not be changed due to CPU itself\n");
     620                 :          0 :                 rte_power_unset_env();
     621                 :          0 :                 return 0;
     622                 :            :         }
     623                 :            : 
     624                 :          0 :         ret = check_power_get_freq();
     625         [ #  # ]:          0 :         if (ret < 0)
     626                 :          0 :                 goto fail_all;
     627                 :            : 
     628                 :          0 :         ret = check_power_set_freq();
     629         [ #  # ]:          0 :         if (ret < 0)
     630                 :          0 :                 goto fail_all;
     631                 :            : 
     632                 :          0 :         ret = check_power_freq_down();
     633         [ #  # ]:          0 :         if (ret < 0)
     634                 :          0 :                 goto fail_all;
     635                 :            : 
     636                 :          0 :         ret = check_power_freq_up();
     637         [ #  # ]:          0 :         if (ret < 0)
     638                 :          0 :                 goto fail_all;
     639                 :            : 
     640                 :          0 :         ret = check_power_freq_max();
     641         [ #  # ]:          0 :         if (ret < 0)
     642                 :          0 :                 goto fail_all;
     643                 :            : 
     644                 :          0 :         ret = check_power_freq_min();
     645         [ #  # ]:          0 :         if (ret < 0)
     646                 :          0 :                 goto fail_all;
     647                 :            : 
     648                 :          0 :         ret = check_power_turbo();
     649         [ #  # ]:          0 :         if (ret < 0)
     650                 :          0 :                 goto fail_all;
     651                 :            : 
     652                 :          0 :         ret = rte_power_exit(TEST_POWER_LCORE_ID);
     653         [ #  # ]:          0 :         if (ret < 0) {
     654                 :            :                 printf("Cannot exit power management for lcore %u\n",
     655                 :            :                                                 TEST_POWER_LCORE_ID);
     656                 :          0 :                 rte_power_unset_env();
     657                 :          0 :                 return -1;
     658                 :            :         }
     659                 :            : 
     660                 :            :         /**
     661                 :            :          * test of exiting power management for the lcore which has been exited
     662                 :            :          */
     663                 :          0 :         ret = rte_power_exit(TEST_POWER_LCORE_ID);
     664         [ #  # ]:          0 :         if (ret == 0) {
     665                 :            :                 printf("Unexpectedly exit successfully power management twice "
     666                 :            :                                         "on lcore %u\n", TEST_POWER_LCORE_ID);
     667                 :          0 :                 rte_power_unset_env();
     668                 :          0 :                 return -1;
     669                 :            :         }
     670                 :            : 
     671                 :            :         /* test of exit power management for an invalid lcore */
     672                 :          0 :         ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
     673         [ #  # ]:          0 :         if (ret == 0) {
     674                 :            :                 printf("Unexpectedly exit power management successfully for "
     675                 :            :                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
     676                 :          0 :                 rte_power_unset_env();
     677                 :          0 :                 return -1;
     678                 :            :         }
     679                 :          0 :         rte_power_unset_env();
     680                 :          0 :         return 0;
     681                 :            : 
     682                 :          0 : fail_all:
     683                 :          0 :         rte_power_exit(TEST_POWER_LCORE_ID);
     684                 :          0 :         rte_power_unset_env();
     685                 :          0 :         return -1;
     686                 :            : }
     687                 :            : 
     688                 :            : static int
     689                 :          0 : test_power_caps(void)
     690                 :            : {
     691                 :            :         struct rte_power_core_capabilities caps;
     692                 :            :         int ret;
     693                 :            : 
     694                 :          0 :         ret = rte_power_init(TEST_POWER_LCORE_ID);
     695         [ #  # ]:          0 :         if (ret < 0) {
     696                 :            :                 printf("Cannot initialise power management for lcore %u, this "
     697                 :            :                         "may occur if environment is not configured "
     698                 :            :                         "correctly(APCI cpufreq) or operating in another valid "
     699                 :            :                         "Power management environment\n", TEST_POWER_LCORE_ID);
     700                 :          0 :                 rte_power_unset_env();
     701                 :          0 :                 return -1;
     702                 :            :         }
     703                 :            : 
     704                 :          0 :         ret = rte_power_get_capabilities(TEST_POWER_LCORE_ID, &caps);
     705         [ #  # ]:          0 :         if (ret) {
     706                 :            :                 printf("POWER: Error getting capabilities\n");
     707                 :          0 :                 return -1;
     708                 :            :         }
     709                 :            : 
     710                 :          0 :         printf("POWER: Capabilities %"PRIx64"\n", caps.capabilities);
     711                 :            : 
     712                 :          0 :         rte_power_unset_env();
     713                 :          0 :         return 0;
     714                 :            : }
     715                 :            : 
     716                 :            : #endif
     717                 :            : 
     718                 :        235 : REGISTER_FAST_TEST(power_cpufreq_autotest, false, true, test_power_cpufreq);
     719                 :        235 : REGISTER_TEST_COMMAND(power_caps_autotest, test_power_caps);

Generated by: LCOV version 1.14