I have a question about a situation that I encounter quite often. From time to time I have to implement various TCP based protocols. Most of them define variable-length data packets that start with a common header ([packet identifier, length, payload] or something really similar). Obviously, there can be two approaches to reading these packages:
Obviously, the first approach is simple, but requires two calls read()(or perhaps more). The second one is a bit more complicated, but requires fewer challenges.
read()
The question is, does the first approach affect performance so badly to worry about it?
The best way to get your answer is to measure. The program straceis suitable for measuring the time of a system call. Using this adds a lot of overhead in itself, but if you just compare the cost of one recvfor this purpose compared to the cost of two, it should be reasonably significant. Use the option -ttto get the time. Or you can use the option -cto get an overview of the time spent separately, with which syscall it was spent.
strace
recv
-tt
-c
, , oprofile.
oprofile
, , , fdopen stdio, . , / , , select/poll - .
fdopen
select
poll
, , . , x86, RISC (, mips,...).
, , .
, , . , . , , memcpy, , , syscall .
memcpy
, , , , - recv(), .
, recv() , , , recv() - . , recv() , , . - . recv(), , , . recv() , .
, , . , recv(). , , 16- 2 . , , recv() , , .
, ( , , ), , recv(). , recv(), , . , , .
, " " , (, ) , recv() - 2.
recv()
, , read/recv . , recv() , . recv() , 4k, 4k-. header + data. , . mem- , , , .