• OSTEP第六章测试


    测试上下文时间

    Homework (Measurement) ASIDE: MEASUREMENT HOMEWORKS Measurement homeworks are small exercises where you write code to run on a real machine, in order tomeasure some aspect of OS or hardware performance. The idea behind such homeworks is to give you a little bit of hands-on experience with a real operating system. In this homework, you’ll measure the costs of a system call and context switch. Measuring the cost of a system call is relatively easy. For example, you could repeatedly call a simple system call (e.g., performing a 0-byte read), and time how long it takes; dividing the time by the number of iterations gives you an estimate of the cost of a system call. One thing you’ll have to take into account is the precision and accuracy of your timer. A typical timer that you can use is gettimeofday(); read theman page for details. What you’ll see there is that gettimeofday() returns the time in microseconds since 1970; however, this does not mean that the timer is precise to the microsecond. Measure back-to-back calls to gettimeofday() to learn something about how precise the timer really is; this will tell you how many iterations of your null system-call test you’ll have to run in order to get a good measurement result. If gettimeofday() is not precise enough for you, you might look into using the rdtsc instruction available on x86 machines. Measuring the cost of a context switch is a little trickier. The lmbench benchmark does so by running two processes on a single CPU, and setting up two UNIX pipes between them; a pipe is just one of many ways processes in a UNIX system can communicate with one another. The first process then issues a write to the first pipe, and waits for a read on the second; upon seeing the first process waiting for something to read from the second pipe, the OS puts the first process in the blocked state, and switches to the other process, which reads from the first pipe and then writes to the second. When the second process tries to read from the first pipe again, it blocks, and thus the back-and-forth cycle of communication continues. By measuring the cost of communicating like this repeatedly, lmbench can make a good estimate of the cost of a context switch. You can try to re-create something similar here, using pipes, or perhaps some other communication mechanism such as UNIX sockets. One difficulty in measuring context-switch cost arises in systems with more than one CPU; what you need to do on such a system is ensure that your context-switching processes are located on the same processor. Fortunately, most operating systems have calls to bind a process to a particular processor; on Linux, for example, the sched setaffinity() call is what you’re looking for. By ensuring both processes are on the same processor, you are making sure to measure the cost of the OS stopping one process and restoring another on the same CPU.

    很巧的是

    在寻找上下文切换readwrite方法找到了测验

    网页

     1     #include <stdio.h>  
     2     #include <stdlib.h>  
     3     #include <sys/time.h>  
     4     #include <time.h>  
     5     #include <sched.h>  
     6     #include <sys/types.h>  
     7     #include <unistd.h>      //pipe()  
     8       
     9     int main()  
    10     {  
    11         int x, i, fd[2], p[2];  
    12         char send    = 's';  
    13         char receive;  
    14         pipe(fd);  
    15         pipe(p);  
    16         struct timeval tv;  
    17         struct sched_param param;  
    18         param.sched_priority = 0;  
    19       
    20         while ((x = fork()) == -1);  
    21         if (x==0) {  
    22             sched_setscheduler(getpid(), SCHED_FIFO, &param);  
    23             gettimeofday(&tv, NULL);  
    24             printf("Before Context Switch Time %u us
    ", tv.tv_usec);  
    25             for (i = 0; i < 10000; i++) {  
    26                 read(fd[0], &receive, 1);  
    27                 write(p[1], &send, 1);  
    28             }  
    29             exit(0);  
    30         }  
    31         else {  
    32             sched_setscheduler(getpid(), SCHED_FIFO, &param);  
    33             for (i = 0; i < 10000; i++) {  
    34                 write(fd[1], &send, 1);  
    35                 read(p[0], &receive, 1);  
    36             }  
    37             gettimeofday(&tv, NULL);  
    38             printf("After Context SWitch Time %u us
    ", tv.tv_usec);  
    39         }  
    40         return 0;  
    41     }  
  • 相关阅读:
    ChartCtrl源码剖析之——CChartTitle类
    ChartCtrl源码剖析之——CChartObject类
    如何在BCGControlBar工程的工具栏里面新增下拉列表控件
    如何在BCGControlBar界面库的CBCGPFormView子视图里面添加工具栏
    记一次调试串口设备Bug的经历
    复数学习笔记
    2021 新高考Ⅰ卷数学试卷及答案
    博客搬家了
    塔伯自我指涉由图片生成K值python程序
    塔伯自我指涉公式作图程序(matplotlib)
  • 原文地址:https://www.cnblogs.com/otakus/p/13181573.html
Copyright © 2020-2023  润新知