Showing posts with label tuning. Show all posts
Showing posts with label tuning. Show all posts

Flamegraphs part 1

 

As a developer/sysadmin you can create flamegraphs to to create visualizations of system performance data recorded with the perf tool. This perf output shows a stack trace followed by a count, for a total of #N number of samples

git clone the flamegraph scripts:

cd /home/ubuntu
git clone https://github.com/brendangregg/FlameGraph

Sampling a go programme which downloads an linuxmint iso

package main
import (
    "fmt"
    "net/http"
    "io"
    "os"
)

func check(e error) {
    if e != nil {
        panic(e)
    }
}

func main() {
    d1 := []byte("helo world\n")
    for i := 0; i < 10000; i++ {
        resp, err := http.Get("https://mirrors.layeronline.com/linuxmint/stable/21/linuxmint-21-cinnamon-64bit.iso")
        //body, err := io.ReadAll(resp.Body)
        fmt.Println(resp.StatusCode)
        check(err)
        defer resp.Body.Close()
        file, err := os.Create("/tmp/hello.iso")
        size, err := io.Copy(file, resp.Body)
        defer file.Close()
        fmt.Printf("downloaded %s with size %d", file, size)
        err = os.WriteFile("/tmp/check.txt", d1, 0644)
        check(err)
    }
}

compile/build the main.go and run it ./main
Get the process id of main e.g ps aux | grep main

perf record -a -F 99 -g -p 1464 -- sleep 20
Running above command creates a perf.data file

perf script > perf.script //it will by default read perf.data from current working directory and redirects stdout to a file perf.script(ascii file)
This command reads the input file and displays the trace recorded.

Creating flame graph:-

./FlameGraph/stackcollapse-perf.pl perf.script  | ./FlameGraph/flamegraph.pl > flame1006.svg

Download and view flame1006.svg in browser

flamegraph for pid 1464

Here we can observe that io.copybuffer function is using most cpu time (io.copy source)
looking further net.(*netFD).Read is the function call using most cpu time. This net.(*netFD).Read implements func (*IPConn) Read
Conn is a generic stream-oriented network connection.
this function reads data from the connection. This func https://go.dev/src/net/http/transfer.go ¶
we also see ksys_read() is called. This function is responsible for retrieving the struct fd that corresponds with the file descriptor passed in by the user. The struct fd structure contains the struct file_operations structure within it.

Image description
sock_read_iter is fired when receiving a message on a socket
By looking at the graph we can conclude that the cpu usage by main programme is spent mostly on reading data from the connection.

zfs tune article

https://docs.oracle.com/cd/E53394_01/html/E54801/zfspools-4.html

http://open-zfs.org/wiki/Performance_tuning

https://pthree.org/2012/12/13/zfs-administration-part-viii-zpool-best-practices-and-caveats/

http://jrs-s.net/2015/02/06/zfs-you-should-use-mirror-vdevs-not-raidz/


tuning nginx compression and decompression

Much of what NGINX has configured for us by default is great, but there are some settings that we can adjust to make the server perform even better. One of the adjustments that we can make is to have NGINX compress resources before sending them to the client. This modification isn’t free, as it does add some CPU overhead, but not enough to outweigh the benefits.
Note: the commands in this video are run as the root user.

Documentation For This Video

Configuring and Enabling GZIP

We came across gzip when we first installed NGINX and looked at the default configuration, but it was commented out. The simplest way to enable compression for our responses is to uncomment that line in our nginx.conf file:
/etc/nginx/nginx.conf (partial)
    gzip on;
That change is simple enough, but there’s more that we can do to make this better by using some of the other directives from the gzip module. By default, we’re not going to compress the responses that we’re getting from proxied servers or any piece of content that isn’t HTML. Let’s make some modifications to compress more of what we know we’ll respond with:
/etc/nginx/nginx.conf (partial)
  gzip on;
  gzip_disable msie6;
  gzip_proxied no-cache no-store private expired auth;
  gzip_types text/plain text/css application/x-javascript application/javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp image/svg+xml;
  gzip_min_length 1024;
  gzip_vary on;
Let’s break down these directives:
  • gzip_disable - Ensure that we’re not sending compressed responses to older versions of Internet Explorer (hopefully no one is actually using these browsers)
  • gzip_proxied - Specify that we only want to compress responses from proxied servers if we normally wouldn’t cache them
  • gzip_types - The Content-Types that we will compress before sending the response
  • gzip_min_length - Adjusting the minimum size of a file that we’ll compress, the default is 20 bytes, but we’re going to not compress resources that are less than one kilobyte
  • gzip_vary - Adds a Vary: Accept-Encoding header. This tells intermediate caches (like CDNs) to treat the compressed and uncompressed version of the same resource as 2 separate entities

What About Decompression

We’re handling on-the-fly compression now, but if one of our proxied servers happens to send us a pre-compressed response then we probably want to decompress it for clients that can’t handle gzip. In this situation, we’ll use the gunzip module, and we’ll place this in the same area as our gzip directives:
/etc/nginx/nginx.conf

  gzip on;
  gzip_disable msie6;
  gzip_proxied no-cache no-store private expired auth;
  gzip_types text/plain text/css application/x-javascript application/javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp image/svg+xml;
  gzip_min_length 1024;
  gzip_vary on;
  gunzip on;

system tuning for database servers

Virtual Memory

Dirty Ratio

Page cache on a Linux system is the area where filesystem based I/O is cached and that these settings affect how the cache is utilized by the kernel.
We can tune pdflush to determine how much RAM based cache to use for (dirty pages) data targeted to disk and how frequently to flush that cached data by writing the pages back to disk.
The “dirty_ratio” is the percentage of total system memory that can hold dirty pages. The default on most Linux hosts is between 20-30%. When you exceed the limit, the dirty pages are committed to disk, creating a small pause. To avoid this hard pause, there is a second ratio: “dirty_background_ratio” (default 10-15%), which tells the kernel to start flushing dirty pages to disk in the background without any pause.
20-30% is a good general default for “dirty_ratio,” but on large-memory database servers, this can be a lot of memory! For example, on a 128GB-memory host, this can allow up to 38.4GB of dirty pages. The background ratio won’t kick in until 12.8GB! We recommend that you lower this setting and monitor the impact to query performance and disk IO. The goal is reducing memory usage without impacting query performance negatively. Reducing caches sizes also guarantees data gets written to disk in smaller batches more frequently, which increases disk throughput (rather than huge bulk writes less often).
A recommended setting for dirty ratios on large-memory (64GB+ perhaps) database servers is: “vm.dirty_ratio = 15″ and “vm.dirty_background_ratio = 5″, or possibly less. (Red Hat recommends lower ratios of 10 and 3 for high-performance/large-memory servers.)
You can set this by adding the following lines to “/etc/sysctl.conf
vm.dirty_ratio=15
vm.dirty_background_ratio=5