Friday, April 30, 2021

Math: Simple technique to implement moving averages in Java, C or C++

 The oneline solution is:

accumulator = (alpha * new_value) + (1.0 - alpha) * accumulator
Here,

Accummulator - holds the value being tracked
alpha - value between 0 and 1.

The more aggressive the alpha (closer to 1) the  faster the moving average adapts to the recent values. This is an exponential moving average.

Thursday, April 29, 2021

Linux: Poking the ethernet driver with the ethtool

 1. Get basic information about the interface

[mylinuxbox@mylinuxbox-linux ~]$ ethtool -i eth4
driver: e1000e
version: 2.1.4-k
firmware-version: 0.13-4
bus-info: 0000:00:19.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no


2. Dump all the hardware registers
[root@mylinuxbox-linux mylinuxbox]# ethtool -d eth4
MAC Registers
-------------
0x00000: CTRL (Device control register)  0x18100240
      Endian mode (buffers):             little
      Link reset:                        normal
      Set link up:                       1
      Invert Loss-Of-Signal:             no
      Receive flow control:              enabled
      Transmit flow control:             enabled
      VLAN mode:                         disabled
      Auto speed detect:                 disabled
      Speed select:                      1000Mb/s
      Force speed:                       no
      Force duplex:                      no
0x00008: STATUS (Device status register) 0x00080083
      Duplex:                            full
      Link up:                           link config
      TBI mode:                          disabled
      Link speed:                        1000Mb/s
      Bus type:                          PCI
      Bus speed:                         33MHz
      Bus width:                         32-bit
0x00100: RCTL (Receive control register) 0x04008002
      Receiver:                          enabled
      Store bad packets:                 disabled
      Unicast promiscuous:               disabled
      Multicast promiscuous:             disabled
      Long packet:                       disabled
      Descriptor minimum threshold size: 1/2
      Broadcast accept mode:             accept
      VLAN filter:                       disabled
      Canonical form indicator:          disabled
      Discard pause frames:              filtered
      Pass MAC control frames:           don't pass
      Receive buffer size:               2048
0x02808: RDLEN (Receive desc length)     0x00001000
0x02810: RDH   (Receive desc head)       0x00000051
0x02818: RDT   (Receive desc tail)       0x00000040
0x02820: RDTR  (Receive delay timer)     0x00000000
0x00400: TCTL (Transmit ctrl register)   0x3003F0FA
      Transmitter:                       enabled
      Pad short packets:                 enabled
      Software XOFF Transmission:        disabled
      Re-transmit on late collision:     disabled
0x03808: TDLEN (Transmit desc length)    0x00001000
0x03810: TDH   (Transmit desc head)      0x0000007A
0x03818: TDT   (Transmit desc tail)      0x0000007A
0x03820: TIDV  (Transmit delay timer)    0x00000008
PHY type:                                unknown

Tuesday, April 27, 2021

Latex: Customizing Footnotes for a neater look

There is a simple hack to do this:
\renewcommand{\thefootnote}{\arabic{footnote}}Arabic numerals, e.g., 1, 2, 3...
\renewcommand{\thefootnote}{\roman{footnote}}Roman numerals (lowercase), e.g., i, ii,... 
\renewcommand{\thefootnote}{\Roman{footnote}}Roman numerals (uppercase), e.g., I, II, ... 
\renewcommand{\thefootnote}{\alph{footnote}}Alphabetic (lowercase), e.g., a, b... 
\renewcommand{\thefootnote}{\Alph{footnote}}Alphabetic (uppercase), e.g., A, ... 
\renewcommand{\thefootnote}{\fnsymbol{footnote}}A sequence of nine symbols,

Changed mine to alphabets. Looks much neater now. 

Monday, April 26, 2021

Linux: Nagle's Algorithm and How to Disable it

 Nagle's algorithm is a TCP optimization in the kernel stack that waits to aggregate small chunks of bytes before sending packets on a TCP connection. This approach optimizes the amount of frame overhead spent in sending very small packets over the network. However, when the data is fairly sporadic, this could also lead to an increase in the average delay experienced.


Nagle's algorithm running on a host can be disabled by:
echo 1 > /proc/sys/net/ipv4/tcp_low_latency

Sunday, April 25, 2021

Linux: Kernel makefile system in brief

 There are multiple components in the kernel build system as shown in the table below:



Components in the Linux kernel build system

The Top makefile builds kernel image and modules. It does this by visiting directories recursively based on the kernel .config. The top Makefile textually includes an arch Makefile with the name arch/$(ARCH)/Makefile. The arch Makefile supplies architecture-specific information to the top Makefile. Each directory has a kbuild makefile which builds built-in or modular targets based on commands passed from above and info n the .config. Makefiles in the kernel are also referred to as kbuild files.

Further reading: A detailed discussion on these is available in the kernel documentation.