Random recording (emulation of multi-core CPUs running on x86 Linux)

Source: Internet
Author: User


"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Friends who have done embedded development know that debugging code in the real world or on the FPGA is a very hard and thankless task. On the one hand, chips on the chip less resources, on the other hand, in addition to the software you also have to deal with a variety of accidents hardware accidents. A better company might also provide a virtual machine to debug, but many times the stability of the virtual machine is also a problem. Friends who have done SkyEye should have more experience.


In fact, if you debug the driver code or the CPU boot code, then there is no doubt that the actual environment should be developed. But if the development of the code is only C content, in fact, the simulation on the x86 can be. x86 debugging environment is extremely stable, and support a lot of functions, in addition x86 to single core, multi-core support is very good, this is a lot of development boards can not be compared.


For example, debugging multicore in a real-world environment is a very tiring thing. But on x86, especially multicore x86, you only need two thread to bind to a different CPU. It's that simple. A simple Linux thread binding code is given below. The method of compiling is GCC start.c-g-o start-lpthread. The original code address is man7.org/linux/man-pages/man3/pthread_setaffinity_np.3.html.

#define _gnu_source#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <errno.h > #define HANDLE_ERROR_EN (EN, msg) do {errno = en; perror (msg); exit (exit_failure);} and (0) intmain (int argc,    Char *argv[]) {int S, J;    cpu_set_t Cpuset;    pthread_t thread;    thread = Pthread_self ();    /* Set affinity mask to include CPUs 0 to 4 */Cpu_zero (&cpuset);    for (j = 0; J < 4; j + +) Cpu_set (j, &cpuset);    s = pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset);    if (s! = 0) Handle_error_en (S, "pthread_setaffinity_np"); /* Check The actual affinity mask assigned to the thread */s = pthread_getaffinity_np (thread, sizeof (cpu_set_t), &    Cpuset);    if (s! = 0) Handle_error_en (S, "pthread_getaffinity_np");    printf ("Set returned by PTHREAD_GETAFFINITY_NP () contained:\n");    for (j = 0; J < Cpu_setsize; J + +) if (Cpu_isset (J, &cpuset)) printf ("CPU%d\n", j);Exit (exit_success);} 

How to see if there is a binding success? You can add a while (1); Code. Then enter the top command, press 1, to see if the corresponding CPU has a usage rate of 100%.



1

Random recording (emulation of multi-core CPUs running on x86 Linux)

Related Article

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.