A Preliminary Study of PHP socket-Starts with libevent (1)

[Original Address:https://blog.ti-node.com/blog …]

Just as the title says, the opening words of epoll are quivering. The reason for shaking and trembling is probably that there was almost no previous experience of “hand-cutting” epoll himself. Only epoll’s theoretical knowledge was used to cheat children into acting as masters. Now, without the care of the elder martial brother’s iron-headed skills, the lightness skill of the six younger martial brothers, and Ah Mui’s Taiji skill to protect themselves, they have to be the first-line soldiers themselves.

At the end of the day, or because of their own virtual.

First set up a flag, that is, epoll is better than select, although select is POSIX standard. Even the high-end version of select poll is much worse than epoll. Today, when the network is so developed, epoll is the hero in solving the c10k problem. This is something that cannot be done. Although epoll was born later, he was born proud, just like Wang Sicong. Select is an ordinary diaosi, spending a little money to dress up oneself is at best a poll. This poll and epoll are one E away. There is no way. The gap is inherent.

Rumor has it that the number of QQ users increased dramatically before epoll was born, but neither select nor select’s high-end version poll could solve their problems. As a result, QQ’s servers had to use UDP protocol to avoid this problem. Until epoll came into being, QQ began to gradually allow users to choose UDP servers or TCP servers in the configuration items in PC clients.

Let’s take a simple example to explain why epoll is better than select (this example should be mentioned in the previous article and played back again today).

If you want to continue practicing omega supreme’s legs, Ah Mui still needs to collect 10 couriers for you. In order to facilitate your memory of these couriers, you recorded 10 couriers on a list and gave them to Ah Mui. But at this time, ah mui obviously didn’t know how to deal with the scene, so whenever she received x express delivery, ah mui copied a list of express delivery and gave it to you and told you: “there are express delivery!” Ah Mui will not tell you how many express deliveries came and which escort agency escorted them. Therefore, you can only compare the 10 express items on the list with the ones received one by one, then give the list to Ah Mui after the comparison is finished, and then Ah Mui will continue to wait.

It’s the 11th of the second year. ah mui has learned wisdom this time. after that match, she has achieved herself and realized her life value. ah mui this year is a brand new ah mui, a shaved ah mui.

If you want to continue practicing omega supreme’s legs, Ah Mui still needs to collect 10 couriers for you. In order to facilitate your memory of these couriers, you recorded 10 couriers on a list and gave them to Ah Mui. But at this time, ah mui obviously got herself and was a sublimated ah mui, so every time she received x (X >= 1) express, ah mui would shout at you: “the iron headgear of senior brother shunfeng escort agency and the eggs of senior brother yuantong escort agency are here!” And you don’t have to go to the list one by one, ah mui will tell you which escort agency escorted which express, and then she will tell her in advance according to you, “if you receive eggs, give it to six younger brothers, and if you receive iron headgear, give it to master elder brothers.” Even if you bought 10,000 couriers, Ah Mui would still use four or two thousand catties to collect couriers, while you only need to practice omega supreme’s legs quietly.

Ah Mui, who shaved her head, was select and did not dare to look at madame.
After shaving her head, ah mui, epoll, can take the devil’s death ball with her bare hands.

Express delivery is equivalent to socket fd, including monitoring socket and connecting SOCKET; That list is the set of fds; Ah Mui is either select or epoll;. You are the current process. When an express arrives, it is equivalent to an fd that is already readable or writable.

Although select solves the problem that a process can read and write multiple fd to some extent, select has the following fatal disadvantages:

  • By default, the number of fd that select can manage is 1024 (Ah Mui can collect 1024 couriers for you at most)
  • Each time select detects that there are read-write fd in the fd set, it will copy the whole fd to you, and then you will poll which fd is read-write one by one.
  • As mentioned above, it will copy the whole fd to you (she copied a copy of the whole list to you). technically speaking, this process is to copy fd from kernel state to user state calling process.
  • As mentioned above, you can only know which fd is readable and writable by polling all FD one by one (after all, there is a courier coming, who are all the others you check the list by yourself)
  • Your own polling process is linear. if there are n fd, then the time complexity must be O(n)

Epoll, on the other hand, has more professional high-end and high-grade technical indicators:

  • In theory, we can handle the unlimited fd (ah mui who can receive countless express delivery)
  • Only the active fd that can read and write (in fact, there are exceptions in the strict sense) are singled out, and the rest fd are ignored.
  • Accelerating Kernel Data Copy with MMAP

In addition, it is necessary to point out two modes of epoll itself:

  • Horizontal trigger. In this way, if X events are detected, the kernel state will copy these events to the user state. Unfortunately, if the user processes only one of the events and the remaining X-1 events are ignored for some reason, the unprocessed X-1 events will still be copied from the kernel state to the user state next time. In doing so, there are two sides of yin and yang, the positive side is safe from loss, and the negative side is a waste of performance. In fact, epoll at this time is quite similar to poll’s working style.
  • Edge trigger. In this way, it is the chicken blood version of epoll, the self-releasing epoll, and should be the correct way to use it. In this case, if x events occur and you only handle one of them, then the remaining X-1 events are considered “lost.” Performance has gone up, and with it is the possible loss of events.

So, you think it’s time to write code to demonstrate epoll, but not for two reasons:

  • Epoll can be directly operated by C language, however, in order to avoid pretending to be forced to fail, I decided not to use C to demonstrate (when going further later)
  • If PHP is used, I have to mention a sad thing.According to my own experience, tell meThat is, PHP cannot directly control epoll. Instead, it needs to handle epoll by operating libevent.

So, what is Libevent? How do you sound familiar, not only familiar, do you look at the figure below, is it still a little familiar? Yes, the front page of this blog is copied.Libevent websiteYes.

First, I copied a passage from the Libevent website: “currently, Libeventsupports/dev/poll, kqueue (2), eventports, posix select (2), windows select (), poll (2), and poll (4). then you can know roughly what libevent is. Libevent encapsulates the API of /dev/poll, kqueue, select, poll and epoll in Mac, shielding some details and differences in the development of these multiplexes and providing a high-performance network event library of unified API.

One more reminder, this thing is written in C language. Decades later, your uncle is still your uncle.

Returning to the straight path is “how to use Libevent in PHP”. In pecl.php.net, there are two extensions that make it easy for phper to control libevent. One is called libevent and the other is called event. We recommend the latter. The former doesn’t know why the version has been stuck in the 0.10 Beta state, while the development date is stuck in the 2013-05-22. I haven’t tried much, and it is estimated that php7 may not be supported, but thanks to the developers. The event extension is quite awesome. the iteration of the version is good. it seems that the developers are quite active and support php7. the current stable version is 2.3.0, so it is recommended to use the event extension.

Just add the installation method of php extension here, taking event extension as an example.

  1. Download the stable version of event 2.3.0, wgethttps://pecl.php.net/get/even …

  1. Unzip tgz source package, tar -zxvf event-2.3.0.tgz

  1. Cd event-2.3.0 enters the home directory, then executes phpize, and then executes. /configure

  1. Execute make

  1. Perform a make install installation.

  1. To configure php’s cli environment configuration file, note that it is not apache2 or fpm, but php.ini of cli, add a sentence: extension =’/usr/lib/PHP/20151012/event.so’, and then execute php -m in the terminal to see if there is an event?

All right, let’s officially call it a day and continue to use php and his event extensions in the next article.

[Original Address:https://blog.ti-node.com/blog …]