⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exstbtop.c

📁 linux环境下嵌入式平台用于top的命令,更实用.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************//* Copyright (c) 2008 NXP B.V.  All rights are reserved.                     *//*                                                                           *//* Redistribution and use in source and binary forms, with or without        *//* modification, are permitted provided that the following conditions        *//* are met:                                                                  *//*                                                                           *//* Redistributions of source code must retain the above copyright            *//* notice, this list of conditions and the following disclaimer.             *//*                                                                           *//* Redistributions in binary form must reproduce the above copyright         *//* notice, this list of conditions and the following disclaimer in the       *//* documentation and/or other materials provided with the distribution.      *//*                                                                           *//* Neither the name of NXP nor the names of its                              *//* contributors may be used to endorse or promote products derived from      *//* this software without specific prior written permission.                  *//*                                                                           *//* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *//* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         *//* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR     *//* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT      *//* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,     *//* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT          *//* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     *//* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     *//* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       *//* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE         *//* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH          *//* DAMAGE.                                                                   *//*                                                                           *//*****************************************************************************//**    @file   exStbTop.c    @brief  STB225 Time Doctor Application@b  Application:  exStbTop*//*Rev Date       Author      Comments--------------------------------------------------------------------------------  1 20080121   neill       Original  2 20080122   neill       Improve maths used in calculations  3 20080205   batelaan    Update file copyright/license text  4 20080213   neill       Add write to file option and get sample size from device  5 20080219   neill       Modify interrupt numbers to match updated kernel  6 20080408   batelaan    Use phStbIpc for -s serverMode option.--------------------------------------------------------------------------------    For consistency and standardisation retain the Section Separators.*//************************************************ INCLUDE FILES                                *************************************************/#include <stdio.h>#include <stdlib.h>#include <stdint.h>#include <stdbool.h>#include <string.h>#include <unistd.h>#include <errno.h>#include <signal.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <sys/mman.h>#include <fcntl.h>#include <tmNxTypes.h>#include <linux/timedoctor.h>#ifdef TMFL_STBIPC#include "phStbIpc_Services.h"#include <pthread.h>#endif/************************************************ LOCAL MACROS                                 *************************************************/#define TIMEDOCTOR_FILE "/dev/timedoctor"#define OUTPUT_FILE_LENGTH (128)#define THRESHHOLD 0#define MAX_TASKS 512#define MAX_IRQS 82#define MAX_NAME 16#define DIV(a, b) (((a) + ((b)/2))/(b))/******************************************************************* LOCAL TYPEDEFS                                                  ********************************************************************/typedef struct _int_stack{    uint32_t  int_task_number;    char_t    name[MAX_NAME];    uint32_t  now;}int_stack;typedef struct _task_info{    uint32_t  task_number;    char_t    name[MAX_NAME];    uint32_t  time_total;}task_info;/******************************************************************* STATIC FUNCTION PROTOTYPES                  <Module>_<Word>+    ********************************************************************//******************************************************************* STATIC DATA                  g[k|p|kp|pk|kpk]<Module>_<Word>+   ********************************************************************/static uint32_t lastValues[3];static bool serverMode = false;#ifdef TMFL_STBIPCstatic pthread_t serverThread;#endifstatic bool timeDoctor_keepAppAlive = true;static int_stack timeDoctor_stack[MAX_IRQS];static int32_t timeDoctor_stackPosition;static uint32_t timeDoctor_intTime[MAX_IRQS];static uint32_t timeDoctor_totalTime;static task_info timeDoctor_tasks[MAX_TASKS];static int32_t  timeDoctor_taskCount;static int32_t timeDoctor_numEntries;static char timeDoctor_outputFile[OUTPUT_FILE_LENGTH];static bool timeDoctor_outputFileEnabled = false;static bool timeDoctor_started;static const char_t* timeDoctor_intName[] = {    "NOT USED 0", //           0    "NOT USED 1", //           1    "NOT USED 2", //           2    "NOT USED 3", //           3    "NOT USED 4", //           4    "NOT USED 5", //           5    "NOT USED 6", //           6    "TIMER", //                7    "NOT USED 8", //           8    "I2C0", //                 9    "I2C1", //                10    "UART0", //               11    "UART1", //               12    "TS_IN0_DV", //           13    "TS_IN0_DMA", //          14    "GPIO", //                15    "AUDIO_DEC", //           16    "VIDEO_DEC", //           17    "CONFIG", //              18    "AOI", //                 19    "SYNC", //                20    "SATA", //                21    "OSD", //                 22    "DISP1", //               23    "DEINTERLACER", //        24    "DISPLAY2", //            25    "VC", //                  26    "SC", //                  27    "IDE", //                 28    "IDE_DMA", //             29    "TS_IN1_DV", //           30    "TS_IN1_DMA", //          31    "SGDX_DMA", //            32    "TS_OUT", //              33    "IR", //                  34    "VMSP1", //               35    "VMSP2", //               36    "PIBC", //                37    "TS_IN0_TRD", //          38    "SGDX_TPD", //            39    "USB", //                 40    "TS_IN1_TRD", //          41    "CLOCK", //               42    "SGDX_PARSER", //         43    "VMSP_DMA", //            44    "MIU", //                 45    "AVCHIP_IRQ", //          46    "SYNC_HD", //             47    "DISP_HD", //             48    "DISP_SCALER", //         49    "OSD_HD1", //             50    "DTL_WRITER_Y", //        51    "DTL_WRITER_C", //        52    "DTL_EM_Y_IR", //         53    "DTL_EM_C_IR", //         54    "DENC_TTX", //            55    "MMI_SIF0", //            56    "MMI_SIF1", //            57    "MMI_CDMMU", //           58    "PIBCS", //               59    "ETHERNET", //            60    "VMSP1_0", //             61    "VMSP1_1", //             62    "VMSP1_DMA", //           63    "TDGR_DE", //             64    "IR1", //                 65    "GPIO_0",//               66    "GPIO_1",//               67    "GPIO_2",//               68    "GPIO_3",//               69    "GPIO_4",//               70    "GPIO_5",//               71    "GPIO_6",//               72    "GPIO_7",//               73    "GPIO_8",//               74    "GPIO_9",//               75    "GPIO_10",//              76    "GPIO_11",//              77    "GPIO_12",//              78    "GPIO_13",//              79    "GPIO_14",//              80    "GPIO_15" //              81};/*********************************************************(((((((*********** EXPORTED DATA      g[k|p|kp|pk|kpk]ph[<lnx|tm|NONE>]StbTemplate_<Word>+ ****************************************************************************//******************************************************************************** FUNCTION IMPLEMENTATION  <Module>[_<Word>+] for static functions             **                          tm[<layer>]<Module>[_<Word>+] for exported functions*********************************************************************************//* *   @brief Handle any registered signal by requesting a graceful exit  */static void signal_handler(int32_t theSignal){    static int32_t caught = 0;    if (caught == 0)     {        caught = 1;        (void)printf("Stopping Application ... (signal %d)\n", theSignal);        timeDoctor_keepAppAlive = false;    }}/* *   @brief Function to return a task entry for the currently active task on the stack  */static __inline__ task_info* findTask(void){    int32_t task;    for(task=0; task<timeDoctor_taskCount; task++)    {        if (timeDoctor_tasks[task].task_number == (timeDoctor_stack[0].int_task_number & 0xFFFF))         {            return &timeDoctor_tasks[task];        }    }    timeDoctor_tasks[task].time_total = 0;    timeDoctor_tasks[task].task_number = (timeDoctor_stack[0].int_task_number & 0xFFFF);    (void)strcpy(timeDoctor_tasks[task].name, timeDoctor_stack[0].name);    timeDoctor_taskCount++;    return &timeDoctor_tasks[task];}/* *   @brief Function to add the elapsed time to an interrupt or task */static __inline__ void addTime(uint32_t now){    uint32_t elapsed;    elapsed = now - timeDoctor_stack[timeDoctor_stackPosition].now;    if (timeDoctor_stackPosition != 0)     {        /* Add the time to the current interrupt */        timeDoctor_intTime[timeDoctor_stack[timeDoctor_stackPosition].int_task_number] += elapsed;    }    else    {        /* Add the time to the current task */        task_info *pTask;        pTask = findTask();        pTask->time_total += elapsed;    }    /* Add the time to the total time */    timeDoctor_totalTime += elapsed;}/* *   @brief Function to push a new event onto the stack (interrupt or task)  */static __inline__ void pushEvent(uint32_t number, const char_t* name, bool isInt, uint32_t now){    timeDoctor_started = true;    /* Add the elapsed time to the current interrupt or task */    addTime(now);    /* Change the stack position */    if (isInt == true)     {        timeDoctor_stackPosition++;    }    else    {        timeDoctor_stackPosition = 0;    }    /* Record the current time for this stack location */    timeDoctor_stack[timeDoctor_stackPosition].int_task_number = number;    timeDoctor_stack[timeDoctor_stackPosition].now = now;    (void)strcpy(timeDoctor_stack[timeDoctor_stackPosition].name, name);}/* *   @brief Function to pop an event from the stack (interrupt or task)  */static __inline__ void popEvent(uint32_t now){    if (timeDoctor_started == true)     {        /* Add the elapsed time to the current interrupt or task */        addTime(now);        /* Pop the top entry off the stack */        if (timeDoctor_stackPosition != 0)         {            timeDoctor_stackPosition--;        }        /* Record the current time for this stack location */        timeDoctor_stack[timeDoctor_stackPosition].now = now;    }}/* *   @brief Function to extract information about the system interupts  */static uint32_t irqInfo(void){    int32_t i;    uint32_t irqTotal = 0;    if ((serverMode == false) && (timeDoctor_outputFileEnabled == false))    {        (void)printf("\nInterrupts:\n");    }    for(i=0; i<MAX_IRQS; i++)    {        if (timeDoctor_intTime[i] != 0)         {            if (DIV(timeDoctor_intTime[i], (timeDoctor_totalTime/100)) > THRESHHOLD)             {                if ((serverMode == false) && (timeDoctor_outputFileEnabled == false))                {                    (void)printf("%-12s    %2d%%\n", timeDoctor_intName[i], DIV(timeDoctor_intTime[i], (timeDoctor_totalTime/100)));                }            }            irqTotal += timeDoctor_intTime[i];        }    }    return irqTotal;}/* *   @brief Function to sort the system tasks by CPU usage  */static void sortTasks(void){    int32_t task;    int32_t limit = timeDoctor_taskCount-1;    bool swap;    do    {        swap = false;        for(task=0; task<limit; task++)        {            if (timeDoctor_tasks[task].time_total < timeDoctor_tasks[task+1].time_total)             {                task_info tmp;                tmp = timeDoctor_tasks[task];                timeDoctor_tasks[task] = timeDoctor_tasks[task+1];                timeDoctor_tasks[task+1] = tmp;                swap = true;            }        }        limit--;    }while(swap==true);}/* *   @brief Function to extract information about the system tasks  */static uint32_t taskInfo(void){    int32_t task;    uint32_t taskTotal = 0;    if ((serverMode == false) && (timeDoctor_outputFileEnabled == false))    {        (void)printf("\nTasks:\n");    }    for(task=0; task<timeDoctor_taskCount; task++)    {        if (timeDoctor_tasks[task].task_number != 0)         {            if (DIV(timeDoctor_tasks[task].time_total, (timeDoctor_totalTime/100)) > THRESHHOLD)             {                if ((serverMode == false) && (timeDoctor_outputFileEnabled == false))                {                    (void)printf("%-8s (%4x) %2d%%\n", timeDoctor_tasks[task].name, timeDoctor_tasks[task].task_number, DIV(timeDoctor_tasks[task].time_total, (timeDoctor_totalTime/100)));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -