tomcat src – org.apache.tomcat.util.Diagnostics.java

this java file is to print out diagnostics for tomcat and jvm system information

the mainly thing is to use java.lang.management.*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// init Bean class to dump system info
	private static final ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();
	private static final CompilationMXBean compilationMXBean = ManagementFactory.getCompilationMXBean();
	private static final OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
	private static final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
	private static final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
 
	private static final PlatformLoggingMXBean loggingMXBean = ManagementFactory
			.getPlatformMXBean(PlatformLoggingMXBean.class);
	private static final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
	private static final List<GarbageCollectorMXBean> garbageCollectorMXBeans = ManagementFactory
			.getGarbageCollectorMXBeans();
	private static final List<MemoryManagerMXBean> memoryManagerMXBeans = ManagementFactory.getMemoryManagerMXBeans();
	private static final List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();

One of examples to print out vmName and vmVersion and so on

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
StringBuilder sb = new StringBuilder();
 
		synchronized (timeformat) {
			sb.append(timeformat.format(new Date()));
		}
		sb.append(CRLF);
 
		sb.append(requestedSm.getString("diagnostics.vmInfoRuntime"));
		sb.append(":" + CRLF);
		sb.append(INDENT1 + "vmName: " + runtimeMXBean.getVmName() + CRLF);
		sb.append(INDENT1 + "vmVersion: " + runtimeMXBean.getVmVersion() + CRLF);
		sb.append(INDENT1 + "vmVendor: " + runtimeMXBean.getVmVendor() + CRLF);
		sb.append(INDENT1 + "specName: " + runtimeMXBean.getSpecName() + CRLF);
		sb.append(INDENT1 + "specVersion: " + runtimeMXBean.getSpecVersion() + CRLF);
		sb.append(INDENT1 + "specVendor: " + runtimeMXBean.getSpecVendor() + CRLF);
		sb.append(INDENT1 + "managementSpecVersion: " + runtimeMXBean.getManagementSpecVersion() + CRLF);
		sb.append(INDENT1 + "name: " + runtimeMXBean.getName() + CRLF);
		sb.append(INDENT1 + "startTime: " + runtimeMXBean.getStartTime() + CRLF);
		sb.append(INDENT1 + "uptime: " + runtimeMXBean.getUptime() + CRLF);
		sb.append(INDENT1 + "isBootClassPathSupported: " + runtimeMXBean.isBootClassPathSupported() + CRLF);
		sb.append(CRLF);

Python Src – How PyMem_GetAllocator() and PyMem_SetAllocator() work

_PyMem_RawMalloc is calling ‘malloc’ to allocate memory and return pointer
so _PyMem_RawMalloc is equal to ‘malloc’

1
2
3
4
5
6
7
8
9
10
11
static void *
_PyMem_RawMalloc(void *ctx, size_t size)
{
    /* PyMem_RawMalloc(0) means malloc(1). Some systems would return NULL
       for malloc(0), which would be treated as an error. Some platforms would
       return a pointer with no memory behind it, which would break pymalloc.
       To solve these problems, allocate an extra byte. */
    if (size == 0)
        size = 1;
    return malloc(size);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
typedef struct {
    /* user context passed as the first argument to the 4 functions */
    void *ctx;
 
    /* allocate a memory block */
    void* (*malloc) (void *ctx, size_t size);
 
    /* allocate a memory block initialized by zeros */
    void* (*calloc) (void *ctx, size_t nelem, size_t elsize);
 
    /* allocate or resize a memory block */
    void* (*realloc) (void *ctx, void *ptr, size_t new_size);
 
    /* release a memory block */
    void (*free) (void *ctx, void *ptr);
} PyMemAllocatorEx;
 
#define PYRAW_FUNCS _PyMem_RawMalloc, _PyMem_RawCalloc, _PyMem_RawRealloc, _PyMem_RawFree // this means
//*ctx = NULL and (*malloc) = _PyMem_RawMalloc = malloc function in c
 
        PyMemAllocatorEx alloc = {NULL, PYRAW_FUNCS}; 
 
// alloc will be assigned to _PyMem_Raw, _PyMem, _PyObject
// for PyMem_GetAllocator, it will get alloc by domain name and pick up one of allocator _PyMem_Raw, _PyMem, _PyObject
        PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
        PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc);
        PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc);
void
PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
{
    switch(domain)
    {
    case PYMEM_DOMAIN_RAW: _PyMem_Raw = *allocator; break;
    case PYMEM_DOMAIN_MEM: _PyMem = *allocator; break;
    case PYMEM_DOMAIN_OBJ: _PyObject = *allocator; break;
    /* ignore unknown domain */
    }
}

There are 3 Domains that are PYMEM_DOMAIN_RAW, PYMEM_DOMAIN_MEM, PYMEM_DOMAIN_OBJ

alloc will be assigned to _PyMem_Raw, _PyMem, _PyObject based on 3 Domains.
for PyMem_GetAllocator, it will get alloc by domain name and pick up one of allocator _PyMem_Raw, _PyMem, _PyObject

Java – Runtime.getRuntime().addShutdownHook()

a useful method when program done and run last step task.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
 
public class AddShutdownHookTest {
 
	static class Message extends Thread {
 
		public void run() {
			System.out.println("Bye.");
		}
	}
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
 
			// register Message as shutdown hook
			Runtime.getRuntime().addShutdownHook(new Message());
 
			// print the state of the program
			System.out.println("Program is starting...");
 
			// cause thread to sleep for 3 seconds
			System.out.println("Waiting for 3 seconds...");
			Thread.sleep(3000);
 
			// print that the program is closing
			System.out.println("Program is closing...");
 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

Nginx – reverse proxy as load-balance in ubuntu

1. install nginx in ubuntu

1
$ sudo apt install ngnix

2. print ngnix conf path

1
$ sudo ngnix -t

3. create node.conf, in nginx.conf, it would load all file matched *.conf under conf.d/

1
2
$ touch /etc/nginx/conf.d/
$ vim /etc/nginx/conf.d/node.conf

4. node.conf file context.
it is best to keep X-Real-IP and X-Forwarded-For, because in node.js app, it would record client’s ip address into log

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
upstream nodeserver {
   server localhost:3085;
}
 
server {
   listen 8080;
   server_name linode.emacslisp.com;
 
   location /test {
       proxy_pass http://nodeserver;   
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
       proxy_redirect     off;
       proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
       proxy_max_temp_file_size 0;
       proxy_connect_timeout      90;
       proxy_send_timeout         90;
       proxy_read_timeout         90;
       proxy_buffer_size          4k;
       proxy_buffers              4 32k;
       proxy_busy_buffers_size    64k;
       proxy_temp_file_write_size 64k;       
   }
}

Emacs – lldb with gud-lldb.el

since gdb is disabled in latest version of mac,
it is not that easy to enable gdb.
although there is way to enable gdb, once mac os is updated, we have to search new ways again.

it seems like we have to use lldb.
there is a few ways to use lldb with UI,
such as eclipse for c/c++ with lldb plugin.

but for emacs, it has a plugin with lldb

https://github.com/ptrv/emacs.d/blob/master/site-lisp/gud-lldb.el

emacs-debug

Tomcat Src – tomcat logging

This is starting of tomcat source code analysis.

Tomcat Log is simple.

the following is main example on how to invoke tomcat logging

1
2
3
4
5
6
7
8
9
10
11
12
13
 
package main;
 
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
 
public class tomcat {
	public static void main(String[] args) {
		Log log = LogFactory.getLog(tomcat.class);
		log.info("this is a info message from tomcat");
		log.debug("this is a debug message from tomcat");
	}
}

but when debugging tomcat src, I find that org.apache.juli.ClassLoaderLogManager is invoked.

Its entry is in catalina.sh as starting up script for tomcat.

1
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager

YaraScanService – How to remove it

after Mac OS is upgraded to High Sierra, YaraScanService which is part of MRT.app would start automatically.
YaraScanService takes about 20GB memory in my macbook pro.

so it would make whole mac os frozen.

Even it is working, it would stop a while and start to work.
User wouldn’t feel smoothly operation.

Here is step on how to remove YaraScanService
1. reboot mac os and press ‘Command + R’ to boot into recovery mode.
2. Click the “Utilities” menu and select “Terminal” to open a terminal window.
3. run command csrutil
# csrutil status
# csrutil disable
4. reboot mac os again, now command such as ‘rm -rf xxx’ will work and won’t receive any deny message.
5. after login into macos, open “Terminal” and run
$ sudo rm -rf /System/Library/CoreService/MRT.app
6. in /System/Library/LaunchDaemons , run
$ grep -nri “mrt” *

and delete all files that are displayed on result
7. in /System/Library/LaunchAgent, run
$ grep -nri “mrt” *

and delete all files that are displayed on result

8. reboot mac os, now do some test to check YaraScanService is started or not.
9. go to recovery mode to enable csrutil
$ csrutil enable

Backtrace – leetcode 17 – Letter Combinations of a Phone Number

this is a very classic backtrace problem.

Idea is simple, use ‘call stack’ to store string “a…z”

stack n – z
.
.
stack 1 – a

if stack.length == targetLenth
loop though stack to get char array to string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
package com.dw.leetcode;
 
import java.util.*;
 
public class Letter_Combinations_of_a_Phone_Number_17 {
	static final char[][] number = { { 'a', 'b', 'c' }, { 'd', 'e', 'f' }, { 'g', 'h', 'i' }, { 'j', 'k', 'l' },
			{ 'm', 'n', 'o' }, { 'p', 'q', 'r', 's' }, { 't', 'u', 'v' }, { 'w', 'x', 'y', 'z' }
	};
 
    public List<String> letterCombinations(String digits) {
    	List<String> result = new ArrayList<String>();
    	if(digits.length() == 0){
            return new ArrayList<>(); 
        }
 
    	foo(digits, 0, 0, digits.length(), new ArrayList<Character>(), result);
    	return result;
    }
 
    public void foo(String digits, int index, int total, int len, List<Character> temp, List<String> result) {
    	if(total == len) {
    		StringBuilder stringBuilder = new StringBuilder();
    		for(Character c: temp) {
    			stringBuilder.append(c);
    		}
    		result.add(stringBuilder.toString());
    		return;
    	}
 
    	int numberIndex = Integer.parseInt(Character.toString(digits.charAt(index)));
 
    	for(char c: number[numberIndex - 2]) {
    		temp.add(c);
    		foo(digits, index+1, total+1, len, temp, result);
    		temp.remove(temp.size() - 1);
    	}
    }
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Letter_Combinations_of_a_Phone_Number_17 s = new Letter_Combinations_of_a_Phone_Number_17();
		System.out.println(s.letterCombinations("234"));
	}
 
}

javascript – download Blob file directly

As we know that for security issue, browser would disable javascript to download file directly.

but today what I want to show is to use javascript to click on ‘a’ link to download file
using Blob

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' });
  if (navigator.msSaveBlob) { // IE 10+
    navigator.msSaveBlob(blob, filename);
  } else {
    var link = document.createElement("a");
    if (link.download !== undefined) { 
      var url = URL.createObjectURL(blob);
      link.setAttribute("href", url);
      link.setAttribute("download", filename);
      link.style.visibility = 'hidden';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  }