Lowest latency notification method between process under Linux - linux

I'm looking for the lowest latency IPC that allow to put one process to sleep and allow other process to wake it.
I'm looking for the lowest latency method. Some possible methods so far:
Writing a byte to a pipe and reading it from it.
Writing a byte to a socket and reading it from it.
Sending a signal (kill) and waiting for it (sigwait)
Using sem_post/sem_wait
Any other better ideas?
Any solution that is Linux specific is fine as well.

Generally... There is almost no difference between the OS methods.
Setup:
Two processes with affinity to two different CPUs.
One process sleeps (nanosleep) for N microseconds measures current time
and then notifies other process.
Other process wakes measures current time and compares it to the client's time.
Average, standard deviation, median and percentile 95 is calculated over 1K samples after warm-up on 100 notifications.
OS: Linux 2.6.35 x86_64
CPU: Intel i5 M460
Results:
Semaphore (sem_wait/sem_post - kernel - futex):
sleep us mean median %95
1 4.98 ±18.7 3.78 5.04
10 4.14 ±14.8 3.54 4.00
100 20.60 ±29.4 22.96 26.96
1000 49.42 ±37.6 30.62 78.75
10000 63.20 ±22.0 68.38 84.38
Signal (kill/sigwait)
sleep us mean median %95
1 4.69 ±3.8 4.21 5.39
10 5.91 ±14.8 4.19 7.45
100 23.90 ±17.7 23.41 35.90
1000 47.38 ±28.0 35.27 81.16
10000 60.80 ±19.9 68.50 82.36
Pipe (pipe + write/read)
sleep us mean median %95
1 3.75 ±5.9 3.46 4.45
10 4.42 ±3.5 3.84 5.18
100 23.32 ±25.6 24.17 38.05
1000 51.17 ±35.3 46.34 74.75
10000 64.69 ±31.0 67.95 86.80
Socket (socketpair +write/read)
sleep us mean median %95
1 6.07 ±3.2 5.55 6.78
10 7.00 ±7.1 5.51 8.50
100 27.57 ±14.1 28.39 50.86
1000 56.75 ±25.7 50.82 88.74
10000 73.89 ±16.8 77.54 88.46
As a reference busy waiting:
sleep us mean median %95
1 0.17 ±0.5 0.13 0.23
10 0.15 ±0.3 0.13 0.19
100 0.17 ±0.3 0.16 0.21
1000 0.22 ±0.1 0.18 0.35
10000 0.38 ±0.3 0.30 0.78

Using the same code provided by #Artyom, but in a more morden hardware.
CPU: i9-9900k, closing C/S/P-state and set scaling policy as performance which keeps cores running at the maximum frequency(~5GHz).
OS: Preempt-RT patched Linux with kernel5.0.21, providing better real-time performance.
CPU affinity: two processes apartly running in two isolated cores, which keep away from irrelevant processes and interrupts AMSP.
Results:
Semaphore (sem_wait/sem_post - kernel - futex):
sleep us mean minimum median %99
1 1.75 ±0.1 1.60 1.74 1.82
10 1.76 ±0.0 1.61 1.75 1.83
100 2.12 ±0.3 1.59 2.24 2.42
1000 2.46 ±0.3 1.75 2.47 2.56
10000 2.45 ±0.1 2.11 2.44 2.54
Signal (kill/sigwait)
sleep us mean minimum median %99
1 2.15 ±0.2 2.00 2.13 2.22
10 2.12 ±0.2 1.93 2.11 2.19
100 2.56 ±0.3 2.00 2.67 2.88
1000 2.90 ±0.3 2.17 2.90 3.01
10000 2.94 ±0.5 2.66 2.89 3.03
Pipe (pipe + write/read)
sleep us mean minimum median %99
1 2.05 ±0.2 1.88 2.03 2.15
10 2.06 ±0.3 1.89 2.04 2.17
100 2.54 ±0.4 1.88 2.63 2.87
1000 2.98 ±0.3 2.27 2.98 3.09
10000 2.98 ±0.3 2.69 2.96 3.07
Socket (socketpair +write/read)
sleep us mean minimum median %99
1 3.11 ±0.4 2.85 3.09 3.22
10 3.14 ±0.1 2.92 3.14 3.25
100 3.66 ±0.5 2.92 3.74 4.01
1000 4.03 ±0.4 3.28 4.03 4.17
10000 3.99 ±0.4 3.64 3.96 4.10
As a reference busy waiting:
sleep us mean minimum median %99
1 0.07 ±0.1 0.06 0.07 0.07
10 0.07 ±0.1 0.06 0.07 0.07
100 0.07 ±0.0 0.06 0.07 0.08
1000 0.09 ±0.1 0.07 0.08 0.09
10000 0.09 ±0.1 0.07 0.09 0.09

Related

Determine the trend of the market over time use Pandas + python3

print(df)
Index Time left Type Price
0 1797.0 4.00 0.83
1 1789.0 4.00 0.83
2 1781.0 4.00 0.83
3 1757.0 4.00 0.83
4 1445.0 4.00 0.83
5 1413.0 NaN NaN
6 1397.0 NaN NaN
7 1389.0 4.00 0.66
8 1381.0 4.00 0.66
9 349.0 4.00 0.66
10 1325.0 4.00 0.66
11 1317.0 4.00 0.61
12 301.0 4.00 0.62
13 1293.0 4.00 0.65
14 1285.0 4.00 0.56
15 1261.0 4.00 0.56
16 1245.0 4.00 0.56
17 1237.0 4.00 0.57
18 1213.0 4.00 0.51
19 1197.0 4.00 0.52
20 1021.0 3.75 0.86
21 933.0 3.75 0.86
22 813.0 3.75 0.85
23 797.0 3.75 0.85
24 781.0 3.75 0.81
25 525.0 3.75 0.82
26 509.0 3.75 0.82
27 269.0 3.75 0.83
28 181.0 3.75 0.82
29 165.0 3.75 0.82
30 157.0 3.75 0.82
31 37.0 3.75 0.83
we can see the Price is going down according to Time left and Type, and value difference
df = df.set_index("Type")
a = (df.max(level='Type')["Price"] - df.min(level='Type')["Price"]).sum()
print(a) # value difference: 0.37
How does pandas understand whether a market is going up or down over Time left(example: pandas return up=1, down=0)
My idea:
index1 = (["Type"].max & ["Price"].max)
index2 = (["Type"].min & ["Price"].max)
if index1 > index2:
return 0 (market down)
else:
return 1( market up)
Or is there any more solution, help me convert the idea into code, I have just learn pandas, plz...
You can find indices of min and max with idxmin and idxmax, then compare them:
# get indices of min and max:
z = df.set_index('Time left').groupby('Type')['Price'].agg(['idxmin', 'idxmax'])
# if index of max < index of min, then it's increasing
# (since it's `Time left`, not `Time`, so it's all reversed)
z = z['idxmax'].lt(z['idxmin']).astype(int).to_frame('is_increasing')
z
Output:
is_increasing
Type
3.75 0
4.00 0

Convert "Empty Dataframe" / List Items to Dataframe?

I parsed a table from a website using Selenium (by xpath), then used pd.read_html on the table element, and now I'm left with what looks like a list that makes up the table. It looks like this:
[Empty DataFrame
Columns: [Symbol, Expiration, Strike, Last, Open, High, Low, Change, Volume]
Index: [], Symbol Expiration Strike Last Open High Low Change Volume
0 XPEV Dec20 12/18/2020 46.5 3.40 3.00 5.05 2.49 1.08 696.0
1 XPEV Dec20 12/18/2020 47.0 3.15 3.10 4.80 2.00 1.02 2359.0
2 XPEV Dec20 12/18/2020 47.5 2.80 2.67 4.50 1.89 0.91 2231.0
3 XPEV Dec20 12/18/2020 48.0 2.51 2.50 4.29 1.66 0.85 3887.0
4 XPEV Dec20 12/18/2020 48.5 2.22 2.34 3.80 1.51 0.72 2862.0
5 XPEV Dec20 12/18/2020 49.0 1.84 2.00 3.55 1.34 0.49 4382.0
6 XPEV Dec20 12/18/2020 50.0 1.36 1.76 3.10 1.02 0.30 14578.0
7 XPEV Dec20 12/18/2020 51.0 1.14 1.26 2.62 0.78 0.31 4429.0
8 XPEV Dec20 12/18/2020 52.0 0.85 0.95 2.20 0.62 0.19 2775.0
9 XPEV Dec20 12/18/2020 53.0 0.63 0.79 1.85 0.50 0.13 1542.0]
How do I turn this into an actual dataframe, with the "Symbol, Expiration, etc..." as the header, and the far left column as the index?
I've been trying several different things, but to no avail. Where I left off was trying:
# From reading the html of the table step
dfs = pd.read_html(table.get_attribute('outerHTML'))
dfs = pd.DataFrame(dfs)
... and when I print the new dfs, I get this:
0 Empty DataFrame
Columns: [Symbol, Expiration, ...
1 Symbol Expiration Strike Last Open ...
Per pandas.read_html docs,
This function will always return a list of DataFrame or it will fail, e.g., it will not return an empty list.
According to your list output the non-empty dataframe is the second element in that list. So retrieve it by indexing (remember Python uses zero as first index of iterables). Do note you can use data frames stored in lists or dicts.
dfs[1].head()
dfs[1].tail()
dfs[1].describe()
...
single_df = dfs[1].copy()
del dfs
Or index on same call
single_df = pd.read_html(...)[1]

Support Vector Method

I have the following dataset as a small part of the big dataset.
PM2.5 is the dependent variable, while the other seven-column
represent the independent variables, AOD, BLH, RH, WS, Prec. and Temp.
I am looking to use the Support Vector Method SVM multiple regression
to find the best fit multiple variable regression equation using the python code.
I will appreciate your help a lot.
PM2.5 AOD BLH RH WS Prec Temp SLP
43.52 0.42 0.39 0.74 1.2 0.4 4.95 1.03
18.4 0.31 0.41 0.71 2.9 0.0 13.4 1.02
53.36 0.30 0.91 0.75 3.21 2.8 17.2 1.01
18.83 0.36 0.29 0.48 1.7 0.6 20.5 1.02
21.2 0.39 0.36 0.52 0.93 0.1 22.0 1.02
12.17 0.15 0.69 0.52 0.55 0.1 18.67 1.01
8.75 0.11 0.42 0.59 4.98 0.1 18.67 1.01
7.7 0.31 0.048 0.52 0.95 0.0 22.44 1.02
6.58 0.05 0.48 0.57 2.75 0.0 32.38 1.02
Data as an xls file is here
Thanks a lot in advance

How to correct Python number presentation and/or precision

The floating point numbers with finite precision are represented with different precision in identical conditions
It is detected and tested on python version 3.x under Linux and Windows. And take the negative effect for the next calculation.
for i in range(100):
k = 1 + i / 100;
print(k)
1.0
1.01
1.02
1.03
1.04
1.05
1.06
1.07
1.08
1.09
1.1
1.11
1.12
1.13
1.1400000000000001
1.15
1.16
1.17
1.18
1.19
1.2
1.21
1.22
1.23
1.24
1.25
1.26
1.27
1.28
1.29
1.3
1.31
1.32
1.33
1.34
1.35
1.3599999999999999
1.37
1.38
1.3900000000000001
1.4
1.41
1.42
1.43
1.44
1.45
1.46
1.47
1.48
1.49
1.5
1.51
1.52
1.53
1.54
1.55
1.56
1.5699999999999998
1.58
1.5899999999999999
1.6
1.6099999999999999
1.62
1.63
1.6400000000000001
1.65
1.6600000000000001
1.67
1.6800000000000002
1.69
1.7
1.71
1.72
1.73
1.74
1.75
1.76
1.77
1.78
1.79
1.8
1.81
1.8199999999999998
1.83
1.8399999999999999
1.85
1.8599999999999999
1.87
1.88
1.8900000000000001
1.9
1.9100000000000001
1.92
1.9300000000000002
1.94
1.95
1.96
1.97
1.98
1.99
It is possible to set the precision in the following way:
for i in range(100):
k = 1 + i / 100;
print("%.Nf"%k)
Where N - decimal numbers.
Keep in mind, that regularly you don't need a lot of them, though the number could be really huge.

I want to store the output of the below command in an variable

I want to store the output of the below command in an variable
This is my cmd
load=$(sar -q | awk -F'[\t] +\\' '{print $1,$2,$3,$4,$5,$6,$7 }')
When I am trying to store the output in an variable I am getting like this inux
3.13.0-45-generic
(vr1tel-Inspiron-3542)
03/27/2015
_i686_
(4
CPU)
06:47:44
AM
LINUX
RESTART
06:55:01
AM
runq-sz
plist-sz
ldavg-1
ldavg-5
ldavg-15
blocked
07:05:01
AM
2
449
1.08
1.01
0.76
0
07:15:01
AM
3
438
1.09
1.11
0.93
0
07:25:01
AM
0
434
0.29
0.69
0.85
0
Average:
2
440
0.82
0.94
but I want answer like this
Linux 3.13.0-45-generic (vr1tel-Inspiron-3542) 03/27/2015 i686 (4 CPU)
06:47:44 AM LINUX RESTART
06:55:01 AM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked
07:05:01 AM 2 449 1.08 1.01 0.76 0
07:15:01 AM 3 438 1.09 1.11 0.93 0
07:25:01 AM 0 434 0.29 0.69 0.85 0
Average: 2 440 0.82 0.94 0.85 0
08:08:13 AM LINUX RESTART
08:34:19 AM LINUX RESTART
08:35:01 AM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked
08:45:01 AM 0 437 0.26 0.51 0.46 1
08:55:01 AM 0 418 0.30 0.32 0.40 0
09:05:01 AM 0 348 1.18 0.60 0.48 0
09:15:01 AM 0 364 0.23 0.55 0.55 0
09:25:01 AM 0 364 0.42 0.39 0.46 0
09:35:01 AM 0 439 0.33 0.26 0.34 0
09:45:01 AM 0 469 0.38 0.40 0.36 0

Resources