Previous articles:

  • Basic knowledge of EventLoop, Webkit, V8 principle analysis

  • Advanced knowledge of EventLoop, Webkit, V8 principle analysis

Linux

Linux kernel space vs. user space

  • Why distinguish between kernel space and user space
    • User space: program space allocated by the operating system. Kernel space: operating system management space (process)
    • Code in user space and kernel space, and constantly switching between the two models, if the call of the operating system kernel functions (such as reading and writing files, process management, network communication, etc.), is in the kernel space, otherwise do not require the system to the kernel to participate in, only CPU or memory consumption code in user space (such as algorithm, etc.)

Programs cannot operate hardware across the operating system and cannot be called across layers

  • User mode vs kernel mode

The state in which the program is executed. When the system kernel is not in use, it is user state; otherwise, it is kernel state

  • How do I get into kernel space from user space
  • Activity of the processor at any given point in time:
    • Runs in user space and executes user processes
    • Runs in kernel space, in process context, and executes on behalf of a particular
    • Run in kernel space, in interrupt context, independent of any process, handling a particular interrupt (execution)

Linux signaling mechanism

  • A soft interrupt signal is used to notify a process that an asynchronous event has occurred. Processes can send soft interrupt signals to each other through the system call kill. The kernel can also signal a process to inform it of an event because of an internal event. Notice that signals are only used to inform a process of what has happened, and do not pass any data to the process. Okay
  • The process receiving the signal has different processing methods for various signals. Treatment methods can be divided into three categories:
    • 1. For the signal to be processed, the process can specify a processing function, the function to process (process received a signal (number) -> find the corresponding function to process)
    • 2. Ignore a signal and do nothing to it as if it never happened. There are two signals that cannot be ignored: SIGKILL(kill signal) and SIGSTOP(process termination). Kill PID Sends a signal to kill a PID to the operating system. The operating system finds the process where the PID resides and notifying the process to terminate. The operating system does not directly reclaim the process, but terminates the process. Finally, the operating system reclaims the resources occupied by the process
    • 3. Retain the default values for the signal processing. The default operation for most signals is to terminate the process. This method is the default operation. A process uses the system call signal to specify the processing behavior of a signal.
  • The kernel processes signals received by a process when a process returns from kernel state to user state. Therefore, when a process is running in kernel mode, the soft interrupt signal does not take effect immediately until it is ready to return to user mode. A process will return to user mode only after it finishes processing signals. There are no unprocessed signals in user mode

In human words: asynchronous operation is inseparable from signal communication, the operating system finished things, need to notify the process through the signal mechanism

  • Use kill -l to view all signal flags

The I/O model in Linux

Synchronous blocking I/O model

  • User-space applications block after a system call until the system call completes (data transfer completes or an error occurs). The application is simply waiting for a response and does not consume CPU
  • It is efficient from a CPU point of view

Synchronize the non-blocking I/O model

  • Non-blocking means that if the I/O operation cannot complete immediately, the application sequence needs to be called several times until the task is complete
  • This can be very inefficient, as most programs have to be busy waiting or trying to do something else until the data is available

Asynchronously blocking I/O model

  • The device is opened in non-blocking mode, and then the application blocks in the SELECT system call, using it to listen for available I/O operations
  • The biggest benefit of a SELECT system call is that you can listen for multiple descriptors, and you can specify which events (readable, writable, error) each descriptor listens for.
  • The main problem with SELECT system calls is that they are inefficient. Although it is an asynchronous notification model, it is not used in high-performance I/O
  • High-performance scenarios typically use epoll system calls

Asynchronous non-blocking I/O model

  • I/O operations can be processed in parallel
  • The read request is returned immediately, indicating that the read operation is successfully started. The application can then do something else before the read operation completes. When the read operation is complete, the kernel can tell the application to read the data, either via a signal or a thread-based callback function
  • A single process can execute multiple I/O requests in parallel because the CPU can process sudoku much faster than the I/O. While one or more I/O requests are waiting to be processed, the CPU can process other tasks or process other completed I/O requests

Linux select model

  • The key to the SELECT model is the unified management and scheduling of multiple sockets in an ordered manner

  • Disadvantages of select model
    • There is a maximum limit on the number of file descriptors that a single process can listen on, usually 1024. Although you can change the number, because select scans file descriptors in a polling manner, the more file descriptors there are, the worse the performance (Linux kernel header file definition #define__FD_SETSIZE 1024)
    • Kernel/user space memory copy problem, select needs to copy a large number of handle data structures, resulting in a large overhead
    • Select returns an array containing the entire handle, and the application needs to traverse the entire array to know which handles have an event, which is inefficient
    • Select is triggered horizontally, and if the application does not complete the I/O operation on a file descriptor that is already in place, each subsequent SELECT call will still notify the process of those file tracers

Linux epoll model

  • Advantages of epoll model:
    • Supports a process to open a large number of socket descriptors
    • I/O efficiency does not decrease linearly as the number of FDS (file descriptors) increases
    • Use Mmap to speed up kernel-user space messaging
  • Epoll works in two modes:
    • LT (level triggered) : This is the default working mode and supports both block and non-block sockets. In this way, the kernel tells the program whether a file descriptor is ready or not, and the program can then perform I/O operations on the ready FD. If the program does nothing, the kernel continues to notify. Therefore, this mode of programming is less likely to make mistakes.
    • ET (edge-triggered) : this is an efficient working mode that supports no-block sockets. In this mode, when the descriptor becomes ready from never ready, the kernel notifies the program via epoll. It then assumes that you already know that the FD is ready and doesn’t send any more or more ready notifications for the FD until the next time new data comes in. To put it bluntly, it means that the kernel does not notify any of the things that have been notified, and the program is responsible for missing or unread data. This mechanism does increase speed, but it is risky

Windows IOCP model

Node.js architecture

Nodejs structure

libuv

There are three main things to do: event loops, I/O, and processes

  • Libuv is written in C and cross-platform, making it ideal for embedding in high-level languages like JS and Python
  • Libuv uses an asynchronous event-driven approach, with the core providing I/O event loops and asynchronous callbacks
  • Libuv’s APIS include time, non-blocking networks, asynchronous file operations, child processes, and more
  • The CPU does not block pending programs while they are waiting for I/O to complete, and libuv provides programming that makes it easy to develop asynchronous programs
  • Libuv document
  • Libuv source

    • Include /uv.h: C header file, which is a library entry, not an executable entry, and an index of functions defined in it
    • SRC directory is the source code

V8 engine source code analysis

Rendering engine and WebKit architecture

  • Rendering engine: Capable of converting HTML/CSS/JS text and corresponding resource files into image structures
  • Type of rendering engine:
    • Tridend(IE)
    • Gecko(FF)
    • Webkit(Sarari,Chrome, Android, etc.) is in a separate process

Js engine and rendering engine

  • The rendering engine uses the INTERFACE of the JS engine to process the logical code and get the results
  • The JS engine accesses the DOM and CSSOM in the rendering engine through the bridge interface

Js engine workflow

V8 with Javascript Core

  • Javascript Core engine is the default JS engine in Webkit and an open source project of Apple. Initially, its performance was not very good. Since 2008, a series of optimizations have been carried out to re-implement the compiler and bytecode interpreter, which greatly improved the engine’s performance. Subsequently, embedded cache, regular expression based JIT, simple JIT, bytecode interpreter and other technologies were introduced, and Javascript Core engine continued to iterate and develop
  • There are a few differences between Javascript Core and V8, the biggest being V8’s addition of an intermediate representation of bytecode and the addition of multiple layers of JIT compilers (simple JIT compilers, DFG JIT compilers, LLVM, etc.) to optimize performance

V8 source code analysis

  • V8 documentation
  • V8 source
  • C++ implementation C++ language quickly understand
  • The code mainly looks at include and SRC
    • C++ can implement multiple inheritance, A inherits both B and C

Join us at ❤️

Bytedance Xinfoli team

Nice Leader: Senior technical expert, well-known gold digger columnist, founder of Flutter Chinese community, founder of Flutter Chinese community open source project, well-known developer of Github community, author of dio, FLY, dsBridge and other well-known open source projects

We look forward to your joining us to change our lives with technology!!

Recruitment link: job.toutiao.com/s/JHjRX8B