Perf tool in Linux Kernel is used to analyze various kinds of performance issues. More information could be accessed here, but this article goes with the typical calling procedure of the built in core function “cmd_record”. Source code of perf could be found in linux/tools/perf/perf.c. Now let’s begin with function main(). You could read this article for a quick review. Don’t panic!

Take perf record -a sleep 3 for example.

Initialization:

1. main()->run_argv()->handle_internal_command()->run_builtin()->

status = p->fn(argc, argv, prefix)->cmd_record()

build a new record struct rec with struct record *rec = &record;

/*record is initialized with the following code*/

static struct record record = { .opts = {

… }, .tool = { … },};

Skip these data structures as you wish but don’t hesitate to look up for the variables later.

__dso__find_by_longname(&dsos->root, name);->
Make a dynamic shared object with “[kernel.kallsyms]” then insert it into &machine->dsos
__dsos__addnew(dsos, name);->
dso__new(name);->
__dsos__add(dsos, dso);

dso__read_running_kernel_build_id(kernel, machine);
Read build_id from /sys/kernel/notes which n_type=3 and n_namesz=3
sysfs__read_build_id(path, dso->build_id, sizeof(dso->build_id)
read_build_id(void *buf, buf_size,dso->build_id, sizeof(dso->build_id), false); // buf_size (stbuf.st_size of /sys/kernel/notes) typical value: 360B
//Symbol-minimal.c
// File note is made of series of structure likestruct { u32 n_namesz; u32 n_descsz; u32 n_type; } *nhdr;
if (nhdr->n_type == NT_GNU_BUILD_ID &&
nhdr->n_namesz == sizeof(“GNU”))
In the first n_namesz stores a pointer points to the name of the very field. See the code here for the whole content of notes file.
Copy the very desc to dso->build_id.
Set dso->has_build_id = true.

machine__get_running_kernel_start(machine, &name);Figure out the start address of _text or _stext in /proc/kallsyms
addr = kallsyms__get_function_start(filename, name); //filename = “/proc/kallsyms”
kallsyms__parse(kallsyms_filename, &args, find_symbol_cb)
Content of /proc/kallsyms filled with lines like 00000000 t fuse_async_req_send
Read each line and find the very line c1000000 T _text | _stext and record the start address probably c1000000 to start address then return the hex value.(3238002688)

machine__create_modules(machine);
modules__parse(modules, machine, machine__create_module)
Get start address and names for all modules.

struct map *map = machine__findnew_module_map(machine, start, name);For each modules, find out whether OS have already has module inserted to machine->dsos, if not, a new dso will be created with passed in module name and then inserted into machine->dsos.struct map *map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION, m.name);if (map == NULL) //Can’t find map, so create one for this module
struct dso *dso = machine__findnew_module_dso(machine, &m, filename);
if (dso != NULL)
Find out if there is existing dso for this module name, if not, create one. Module numbers linked to dso is counted by dso->refcnt.
struct map *map = map__new2(start, dso, MAP__FUNCTION);
map_groups__insert(&machine->kmaps, map);

dso__kernel_module_get_build_id(map->dso, machine->root_dir);
Still for each modules, read /sys/module/[MODULE_NAME]/notes/.note.gnu.build-id just like we did in sysfs__read_build_id(path, dso->build_id, sizeof(dso->build_id) in Symbol-minimal.c
sysfs__read_build_id(filename, dso->build_id, sizeof(dso->build_id)

Created fds are stored in evsel->fd->contents[].
Now perf got all fd with syscall in kernel space, we need to mmap them to userspace.
perf_evlist__mmap_ex(evlist, opts->mmap_pages, false,
opts->auxtrace_mmap_pages,(opts->auxtrace_snapshot_mode<0))
perf_evlist__mmap_ex(evlist, 4294967295, false, 0, false)

Then insert fork_event, comm_event to perf.data.
For the main thread, create an mmap_event and read /proc/pid/maps, each line printed will be recorded and then written into perf.data in terms of mmap_event.

Why is that?
gevent.ssl.SSLSocket.__init__() tries to reference SSLContext which doesn’t exist in Python 2.7.8 yet, but it’s introduced in Python 2.7.9.
Bug fixed in the newest version of Python-gevent.
See here for more details.

Got my vps ddosed the other days, and so busy was my working days that I couldn’t spare my time to fix it. Now you see it just works fine, this article write down how I did.

On July 1st I received an email from my vps provider and they told me that I have over 10GB anonymous bit torrent data up/down load per day, hence they have to shut it down to avoid legal issues. Also, I was told that the hacker may left a backdoor even if I change my root password, so I could only reinstall my VPS system. The problem now is: how I could do to save my old home page. Luckily, they offered me an rescue mode, with which I can mount my old disk image, what’s worse is that I couldn’t use chroot. Terrible things just happened.

Second, install a new operating system, and install the following programs.apt-get install apache2 php5 mysql-server mysql-common mysql-clientapt-get install libapache2-mod-auth-mysql php5-mysql php5-gd
Start apache serviceservice apache2 start
install phpmyadmin and wordpressapt-get install phpmyadmin wordpress
Now I have wordpress in /usr/share/wordpress.
Upload settings to the corresponding positions.scp www.haodong.org root@dong:/etc/apache2/site-available/a2ensite www.haodong.orgIt seems apache2 has made a limitation on its web config file that it must ends with .conf so rename www.haodong.org to haodong.conf if you got “www.haodong.org does not exist”For /etc/apache2, which includes default website config containing /var/www/html, make my website work by a2dissite 000-default.conf since in apache2.conf there is IncludeOptional sites-enabled/*.conf

Make sure:1. define(‘WPLANG’, ‘zh_CN’); exist in /etc/wordpress/wp-config.php
2. Enable write permition of htaccess since I need permenant link.
3. The location in /etc/apache2/sites-available/www.haodong.org is just the wordpress symbolic setting file.
4. Be careful on /etc/wordpress/config-xxx.php, which stored the name and password to mysql data file. Make sure you put them in a safe place, and if not, don’t hesitate to crash your head on a wall. :p
Technically, we finished reinstalling and enabling everthing, let’s try.
1. Open http://haodong.org/phpadmin and input your mysql user and password.(Should be set when you install mysql, like root:123456), if you forget everything about mysql, you could reset DB_NAME, DB_USER, DB_PASSWORD bymysql -u root -pCREATE DATABASE wordpress;CREATE USER wordpressuser@localhost IDENTIFIED BY ‘password’;GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost;FLUSH PRIVILEGES;
2. upload the sql file we set aside, remember wordpress only support upload file under 2MB, so we need to gzip it to a xxx.sql.zip. After that, open http://haodong.org/wp-admin/install.php, set an administrator user:password pair, which will be stored in the wp-users table we just imported.
Should everything be done?
No.
3. Open www.haodong.org, oops, I forget my administrator user and password. How should I do? Edit the table wp-users table and set password field to “5d41402abc4b2a76b9719d911017c592” to reset my password to “hello”.
Login, and see how my new website going, but what I saw is an amount of messy code. Actually, the old mysql data file is stored in latin1, so before upload the data file, I need to set it to UTF8 in order to make Chinese characters looks well.vi xxx_database.sql:%s/latin1/UTF8/g:wq
gzip xxx.sql and upload the file again with UTF-8 Unicode (utf8) and utf8_general_ci
Here we got beautiful Chinese words. But wait, from ssh, I still saw Chinese names abnormly showed in my screen.
vi /var/lib/locales/supported.d/local
add
zh_CN.UTF-8 UTF-8en_US.UTF-8 UTF-8zh_CN.GBK GBKzh_CN GB2312
vi /etc/environment
add
LANG=”zh_CN:UTF-8″
LANGUAGE=”zh_CN:zh:en_US:en”
LC_CTYPE=”zh_CN:UTF-8″
Then run locale-gen
Untill now, I could get access to all of my articles when I open my website. But there is one more thing. The media files could still not be touched. I could change uploads file to 777 but this may cause security issues, actually I don’t want to save my vps again. So what I do is:
Change uploads file to 777 temporarily and upload one picture, log on to my vps and see who owns wordpress as a user. And I got:
# ll wordpress/wp-conteng/uploads/xxx.jpg
www-data:www-data
OK, gotcha,
chown -R www-data:www-data /usr/share/wordpress
chmod 755 /usr/share/wordpress
Now everything works well.
Oh, there is still one more thing, I’d like to talk about is the security problem, I need to do something to avoid being hacked again.