The real operation principle of statistical task "reprint"

Source: Internet
Author: User

In Baidu saw Zhang hired articles, Ucos two system task analysis is very appropriate, is the last side of a point of view I do not agree, thank you very much Zhang hired.

First write the original in the ucosii here, and then say my specific understanding (below is my understanding part).

I think this article on Ucos two system task analysis is very appropriate, is the last side of the point of view I do not agree, thank you very much Zhang hired.

First write the original in the ucosii here, and then say my specific understanding.

Μc/os-ⅱ has a task that provides run time statistics. This task is called Os_taskstat () if the user defines a system-defined constant Os_task_stat_en (see file Os_cfg. H) set to 1, this task will be established. Once allowed, Os_taskstat () runs once per second (see File Os_core. C) to calculate the current CPU utilization. In other words, Os_taskstat () tells the user how much CPU time the application uses, expressed as a percentage, which is placed in a signed 8-bit integer oscpusage, with a reading level of 1%.

If the user application intends to use statistical tasks, the user must establish a unique task at initialization, calling Osstatinit () in this task. In other words, before invoking the system startup function Osstart (), the user's initial code must first establish a task in which the system statistics initialization function Osstatinit () is called and then other tasks in the application are established. The program manifest L3.12 is the schematic code of the statistical task.

The program manifest L3.12 initializes the statistics task.

void Main (void)

{

Osinit ();

Osstart ();

}

void Taskstart (void *pdata)

{

Osstatinit ();

for (;;) {

}

}

Because the user's application must first establish a starting task [Taskstart ()], when the main program main () calls the system startup function Osinit (), Μc/os-ⅱ only 3 tasks to manage: Taskstart (), Ostaskidle (), and Os_ Taskstat (). Please note that the name of the task Taskstart () is irrelevant and can be called by any name. Because Μc/os-ⅱ has set the priority of the idle task to the lowest, i.e. Os_lowest_pr10, the priority of the statistics task is set to the lower, os_lowest_pr10 minus 1. Start Task Taskstart () is always the highest priority task.

Figure F3.4 explains the process when initializing the statistics task. The user must first call the system initialization function Osinit () in Μc/os-ⅱ, which initializes the μc/os-ⅱ[diagram F3.4 (2)]. Some processors (such as Motorola's MC68HC11) do not require a "set" interrupt vector, and the interrupt vector is already in ROM. The user must call Ostaskcreat () or ostaskcreatext () to establish Taskstart () [Figure F3.4 (3)]. After the condition of the multi-tasking is ready, call the system startup function Osstart (). This function will cause taskstart () to start executing because Taskstart () is the highest-priority task [Figure F3.4 (4)]].

Taskstart () is responsible for initializing and starting the clock ticks [figure F3.4 (5)]. It is necessary to start the clock tempo here, because the user does not want to receive a clock beat interrupt when multitasking is not yet started. Next Taskstart () calls the statistical initialization function osstatinit () [Figure F3.4 (6)]. The statistical initialization function, Osstatinit (), determines how fast the count of idle counters (OSIDLECTR) is when no other application task runs. When the Pentium II microprocessor operates at 333MHz, the 1 operation allows the counter to reach a value of 15,000,000 times per second. The value of osidlectr is far from the overflow limit value of 32-bit counter 4,294,967,296. Microprocessors are getting faster, and users should be aware that this could be a potential future problem.

The System Statistics initialization task function Osstatinit () calls the delay function ostimedly () delays itself by 2 clock ticks to stop its own operation [figure F3.4 (7)]. This is to synchronize the Osstatinit () with the clock beats. Μc/os-ⅱ then select the next highest priority to go to the ready state of the task run, which is exactly the statistical task Ostaskstat (). The reader will read the Code of Ostaskstat () later, but for a cursory look, the first thing Ostaskstat () should do is to see if the Statistical task readiness flag is "false" and, if so, to delay the two clock ticks [figure F3.4 (8)]. This must be the case because the flag Osstatrdy has been initialized to "false" by the Osinit () function, so actually DSTASKSTAT will also push itself into the sleep state (sleep) two clock beats [figure F3.4 (9)]. Then the task switches to the idle task, and Ostaskidle () starts running, which is the only ready state task. The CPU is in idle task Ostaskidle until the delay of Taskstart () is two clock beats complete [figure 3.4 (10)]. After two clock beats, Taskstart () resumes running [figure F3.4 (11)]. When executing osstartinit (), the idle counter osidlectr is cleared 0 [figure F3.4 (12)]. Osstatinit () Then delays itself for a full second [figure F3.4 (13)]. Because there is no other task to enter the ready state, Ostaskidle () gains control of the CPU [figure F3.4 (14)]. After one second, Taskstart () continues to run, or in Osstatinit (), the idle counter stores the value counted in 1 seconds in the idle counter maximum value Osidlectrmax [Figure F3.4 (15)].

Osstarinit () Sets the statistics Task readiness flag Osstatrdy to True [figure F3.4 (16)] to allow two clock beats to start calculating CPU utilization after Ostaskstat ().

The code for the initialization function of the statistics task, osstatinit (), is shown in program listing L3.13.

The program manifest L3.13 the initialization of the statistics task.

void Osstatinit (void)

{

ostimedly (2);

Os_enter_critical ();

osidlectr = 0L;

Os_exit_critical ();

ostimedly (OS_TICKS_PER_SEC);

Os_enter_critical ();

Osidlectrmax = osidlectr;

Osstatrdy = TRUE;

Os_exit_critical ();

}

The Code for the Statistics task Osstat () is shown in Listing L3.14. In the previous paragraph, it has been discussed why you should wait for the statistics task Ready flag osstatrdy[l3.14 (1)]. This task executes once per second to determine how much CPU time is consumed by tasks in all applications. When the user's application code is added, there is less CPU time to run the idle task, and the osidlectr will not count as much as the original task does not run. To be aware, the maximum value of osidlectr is Osstatinit () is saved in the counter maximum value Osidlectrmax at initialization time. The CPU utilization (expression [3.1]) is stored in the variable oscpusage[l3.14 (2)]:

[3.1] expression need to typeset the equation.

Once the above calculation is complete, ostaskstat () invokes the task statistics external access function Ostaskstathook () [L3.14 (3)], which is a user-definable function that enables the statistical task to be extended. This allows the user to calculate and display the total execution time of all tasks, the percentage of execution time per task, and other information (see 1.09, Example 3).

Program Inventory L3.14 Statistics Tasks

void Ostaskstat (void *pdata)

{

INT32U run;

int8s usage;

pdata = pdata;

while (Osstatrdy = = FALSE) {(1)

ostimedly (2 * os_ticks_per_sec);

}

for (;;) {

Os_enter_critical ();

Osidlectrrun = osidlectr;

Run = osidlectr;

osidlectr = 0L;

Os_exit_critical ();

if (Osidlectrmax > 0L) {

Usage = (int8s) (100l-100l * Run/osidlectrmax); (2)

if (Usage > 100) {

Oscpuusage = 100;

} else if (usage < 0) {

oscpuusage = 0;

} else {

Oscpuusage = usage;

}

} else {

oscpuusage = 0;

}

Ostaskstathook (); (3)

ostimedly (OS_TICKS_PER_SEC);

}

}

Or first carefully look at the source code, so that you can understand the statistical task of the implementation principle and application methods.

Let me say my personal understanding:

Statistical task implementation principle is generally the case, the beginning of the statistical task to initialize, mainly to ensure that the Μc/os-ⅱ idle task on time to run 1s time. Let's see what this mission is all about.

void Ostaskidle (void *pdata)

{

pdata = pdata;

for (;;) {

Os_enter_critical ();

osidlectr++;

Os_exit_critical ();

}

}

This task is actually to give a osidlectr this variable plus one. This allows the idle task to run 1s of time, the 1s within the idle task of the OSIDLECTR variable to reach the maximum value of this boot. It also assigns the value of this variable to the OSIDLECTRMAX variable in the initialization statistics task. This value is constant if not restarted. Use this value to calculate the CPU utilization after other tasks. The exact how to implement the calculation and how to achieve the beginning of the idle task to run 1s. Can look at the above source code. Among them, I marked in red font, I think it should be 2 minutes. Look at the source code will know.

as for, after starting a multitasking, how the statistical task calculates the CPU utilization, about the book said the statistical task to execute once a second problem, I think this statement is not correct, the statistical task is not run every second, you can think about, the statistical task is executed to the last ostimedly (Os_ TICKS_PER_SEC); Such a function. indicates a delay of one minute. Only idle tasks can run when the statistics task is delayed, so whenever the statistics task runs, the value of OSIDLECTR is the number that is recorded in the possible 1s. Because the statistics task is zeroed out after the assigned value. So, the statistical task is not 1s run once, but the statistical task is used to run the variables are data within 1s. Therefore, statistical tasks can be used to count CPU utilization.

The real operation principle of statistical task "reprint"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.