Friday, February 27, 2009

website speed up

  • page analysis tool
http://www.websiteoptimization.com/services/analyze/
  • tutorial

http://www.sitepoint.com/article/web-site-optimization-steps/
http://developer.yahoo.com/performance/rules.html
  • SEO
http://www.seomoz.org

Wednesday, February 25, 2009

msn messager robot

http://flumpcakes.co.uk/php/msn-messenger

http://www.findmysoft.com/scripts/msn_web_messenger_script/

http://www.phpclasses.org/browse/package/1407.html

get contact list from email account

http://drupal.org/node/297877

http://forums.pligg.com/modification-tutorials/9637-contact-import-msn-yahoo-gmail-myspace.html

http://www.e-citizen.com/contacts/libs/

http://www.dominican.eventsworldwide.net/include/contactgrabber/contacts/libs/


http://forums.devarticles.com/php-development-48/import-email-addressess-from-yahoo-gmail-hotmail-86529.html

http://www.phphulp.nl/php/print/4/1355/0/scripts/

Tuesday, February 24, 2009

configure culture in symfony

./apps/frontend/config/settings.yml: default_culture: fr

./apps/frontend/config/i18n.yml: default_culture: en

./apps/frontend/config/factories.yml:# default_culture: %SF_DEFAULT_CULTURE%

Monday, February 23, 2009

gmail contactor list

setup python
http://code.google.com/support/bin/answer.py?answer=75582

http://code.google.com/apis/contacts/docs/1.0/developers_guide_python.html

http://code.google.com/apis/gdata/articles/php_client_lib.html
http://code.google.com/apis/gdata/samples.html


http://code.google.com/apis/contacts/
http://code.google.com/apis/contacts/docs/2.0/developers_guide_protocol.html

cookie in symfony

http://www.symfony-project.org/cookbook/1_0/en/cookie
http://www.symfony-project.org/book/1_0/06-Inside-the-Controller-Layer
http://us2.php.net/setcookie

Saturday, February 21, 2009

html table style

<style>
th{background-color:#bbbbbb;}
td{padding-left:5px;padding-top:5px;}
</style>

Friday, February 20, 2009

svn command

svn log -v
svn revert --recursive .

Thursday, February 19, 2009

ssh to remote host without password

ssh-keygen -t dsa
ssh-copy-id -i ~/.ssh/id_dsa.pub user_name@remote_host_name

Wednesday, February 18, 2009

spin lock perfer same chip core

typedef struct{
int __triger;
volatile int spin;
volatile int flags[800];
volatile int queueLast;
volatile int owner;
volatile int count;
}my_spinlock_t;

int my_spin_init(my_spinlock_t * s, void *);

void my_spin_lock(my_spinlock_t * s);

void my_spin_unlock(my_spinlock_t * s);

void my_spin_dump(my_spinlock_t * s);


#define HAS_LOCK 0
#define MUST_WAIT 1
#define BACKOFF_BASE 100000


inline int g_atomic_int_exchange_and_add (int *atomic, int val)
{
int result;
__asm__ __volatile__ ("lock; xaddl %0,%1"
: "=r" (result), "=m" (*atomic)
: "0" (val), "m" (*atomic));
return result;
}

inline int g_atomic_int_compare_and_exchange (int *atomic, int oldval, int newval)
{
int result;

__asm__ __volatile__ ("lock; cmpxchgl %2, %1"
: "=a" (result), "=m" (*atomic)
: "r" (newval), "m" (*atomic), "0" (oldval));

return result;
}

int my_gettid()
{
int * p = (int*)pthread_self();
return p[18];
}


int my_spin_init(my_spinlock_t * s, void *p)
{
s->spin = HAS_LOCK;
return 0;
}

void my_spin_lock(my_spinlock_t * s)
{
volatile int i;
int b;
int cur_id, my_id = my_gettid();
my_id = my_id%8/2+1;

while( cur_id = g_atomic_int_compare_and_exchange(&s->spin, HAS_LOCK, my_id) ){
b = BACKOFF_BASE;
if(cur_id == my_id) b=0;
while(s->spin != HAS_LOCK)
for(i=b;i;i--);
}
}

void my_spin_unlock(my_spinlock_t * s)
{
s->spin = HAS_LOCK;
}

simple spin lock with backoff delay

typedef struct{
volatile int spin;
}my_spinlock_t;

int my_spin_init(my_spinlock_t * s, void *);

void my_spin_lock(my_spinlock_t * s);

void my_spin_unlock(my_spinlock_t * s);

void my_spin_dump(my_spinlock_t * s);

#define HAS_LOCK 0
#define MUST_WAIT 1
#define BACKOFF_BASE 100000


inline int g_atomic_int_exchange_and_add (int *atomic, int val)
{
int result;
__asm__ __volatile__ ("lock; xaddl %0,%1"
: "=r" (result), "=m" (*atomic)
: "0" (val), "m" (*atomic));
return result;
}

inline int g_atomic_int_compare_and_exchange (int *atomic, int oldval, int newval)
{
int result;

__asm__ __volatile__ ("lock; cmpxchgl %2, %1"
: "=a" (result), "=m" (*atomic)
: "r" (newval), "m" (*atomic), "0" (oldval));

return result == oldval;
}



int my_spin_init(my_spinlock_t * s, void *p)
{
s->spin = HAS_LOCK;
return 0;
}

void my_spin_lock(my_spinlock_t * s)
{
volatile int i;
while( !g_atomic_int_compare_and_exchange(&s->spin, HAS_LOCK, MUST_WAIT) ){
for(i=BACKOFF_BASE;i;i--);
}
}

void my_spin_unlock(my_spinlock_t * s)
{
s->spin = HAS_LOCK;
}

void my_spin_lock(my_spinlock_t * s)
{
volatile int i;
while( !g_atomic_int_compare_and_exchange(&s->spin, HAS_LOCK, MUST_WAIT) ){
while(s->spin == MUST_WAIT)
for(i=BACKOFF_BASE;i;i--);
}
}

simple spin lock

typedef struct{
int spin;
}my_spinlock_t;

int my_spin_init(my_spinlock_t * s, void *);

void my_spin_lock(my_spinlock_t * s);

void my_spin_unlock(my_spinlock_t * s);

void my_spin_dump(my_spinlock_t * s);

#define HAS_LOCK 0
#define MUST_WAIT 1


inline int g_atomic_int_compare_and_exchange (int *atomic, int oldval, int newval)
{
int result;

__asm__ __volatile__ ("lock; cmpxchgl %2, %1"
: "=a" (result), "=m" (*atomic)
: "r" (newval), "m" (*atomic), "0" (oldval));

return result == oldval;
}


int my_spin_init(my_spinlock_t * s, void *p)
{
s->spin = HAS_LOCK;
return 0;
}

void my_spin_lock(my_spinlock_t * s)
{
while( !g_atomic_int_compare_and_exchange(&s->spin, HAS_LOCK, MUST_WAIT) );
}

void my_spin_unlock(my_spinlock_t * s)
{
s->spin = HAS_LOCK;
}

queue-based spin lock

typedef struct{
volatile int flags[80];
volatile int queueLast;
volatile int owner;
}my_spinlock_t;

int my_spin_init(my_spinlock_t * s, void *);

void my_spin_lock(my_spinlock_t * s);

void my_spin_unlock(my_spinlock_t * s);


#define HAS_LOCK 0
#define MUST_WAIT 1


int g_atomic_int_exchange_and_add (int *atomic, int val)
{
int result;
__asm__ __volatile__ ("lock; xaddl %0,%1"
: "=r" (result), "=m" (*atomic)
: "0" (val), "m" (*atomic));
return result;
}

int my_gettid()
{
int * p = (int*)pthread_self();
return p[18];
}


int my_spin_init(my_spinlock_t * s, void *p)
{
int i;
s->flags[0] = HAS_LOCK;
for(i=1;i<8;i++)
s->flags[i*10] = MUST_WAIT;
s->queueLast = 0;
return 0;
}

void my_spin_lock(my_spinlock_t * s)
{
int myPlace, t;
volatile int * p;


myPlace = g_atomic_int_exchange_and_add(&s->queueLast, 1);
//myPlace = my_gettid();

t = myPlace % 8;

p = &s->flags[t*10];
while(*p == MUST_WAIT);
s->flags[t*10] = MUST_WAIT;
s->owner = myPlace;
}

void my_spin_unlock(my_spinlock_t * s)
{
int myPlace = s->owner;
s->flags[((myPlace+1) % 8)*10] = HAS_LOCK;
}



Reference
Thomas E. Anderson, "The Performance of Spin Lock Alternatives for Shared-Memory Multiprocessors", IEEE Transactions on Parallel and Distributed Systems, 1990

Tuesday, February 17, 2009

atomic operation function in C language

http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html
http://library.gnome.org/devel/glib/2.16/glib-Atomic-Operations.html
http://java.sun.com/javase/6/docs/api/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.html
https://stage.maemo.org/svn/maemo/projects/haf/branches/glib2.0/2.8.6-1osso5/glib/gatomic.c
http://www.sogou.com/labs/report/4-2.pdf

Sunday, February 15, 2009

div's position and overlap

<div style="width: 150px; height: 100px; background-color: blue;">
blue
</div>
<div style="width: 100px; height: 150px; background-color: red;">
red
</div>



blue


red



<div style="width: 150px; height: 100px; background-color: blue;position: absolute;">
blue
</div>
<div style="width: 100px; height: 150px; background-color: red; position: relative;">
red
</div>



blue


red



<div style="width: 150px; height: 100px; background-color: blue;position: absolute;z-index:2;">
blue
</div>
<div style="width: 100px; height: 150px; background-color: red; position: relative;z-index:1;">
red
</div>



blue


red

ext js library

Firfox error message: ext is not defined

<link type="text/css" rel="stylesheet" href="/js/ext-2.2/resources/css/ext-all.css"/>
<script type="text/javascript" src="/js/ext-2.2/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="/js/ext-2.2/ext-all.js"></script>

Thursday, February 12, 2009

vsftpd config

  • add user
useradd -d /dir -s /sbin/nologin ftpusername
passwd ftpusername

Wednesday, February 11, 2009

SQL sample

1. one table join itself
select file.id from history t1, history t2, file where file.id=t1.file_id and t1.id<>t2.id and t1.updated_at=t2.updated_at and t1.ip=t2.ip and t1.file_id<>114 and t2.file_id=114 group by t1.file_id order by count(t1.file_id) desc;

Monday, February 9, 2009

URL rewrite

This summary is not available. Please click here to view the post.

Apache log setting

LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog logs/access_log common

<virtualhost>
ServerName xxxx
ServerAlias xxxx.com
DocumentRoot /home/www/xxxx.com/web
AddType application/x-httpd-php .php
<directory>
AllowOverride All
Order allow,deny
Allow from all
</directory>
RewriteLog /home/www/xxxx.com/log/xxxx.com_rewrite_log
RewriteLogLevel 3
ErrorLog /home/www/xxxx.com/log/xxxx.com_error_log
CustomLog /home/www/xxxx.com/log/xxxx.com_access_log common
</virtualhost>


Reference
http://httpd.apache.org/docs/1.3/logs.html

Linux system call

include/asm/unistd_32.h
arch/x86/kernel/syscall_table_32.S

#include <linux/unistd.h>
#define __NR_my_kernel_init 327

//_syscall1(int, my_kernel_init, int, debug);
int my_kernel_init(int debug)
{
return syscall(__NR_my_kernel_init, debug);
}
int main()
{
int ret = my_kernel_init(1);
printf( "%d\n", ret);
return 0;
}

when I using _syscall1(int, my_kernel_init, int, debug), I got an error:
test.c:6: error: expected declaration specifiers or ‘...’ before ‘my_kernel_init’
test.c:6: error: expected declaration specifiers or ‘...’ before ‘debug’
test.c:6: warning: data definition has no type or storage class


Reference:
Linux system call list

Kernel command using Linux system calls, Explore the SCI and add your own calls

Saturday, February 7, 2009

MySql error

  • Can't open file: 'xxxx.MYI' (errno: 13)
perror 13
OS error code 13: Permission denied
chmod 777 xxxx.MYI
  • Repair MySql database table index file
myisamchk -r -q xxxx.MYI
  • MySQL said: Documentation
    #1114 - The table 'xxxx' is full
table xxxx is too large
  • ERROR 1030 (HY000): Got error 127 from storage engine
perror 127
MySQL error code 127: Record-file is crashed

  • repair table xxx USE_FRM;
Reference
http://bbs.ylqonline.net/redirect.php?tid=332&goto=lastpost

Thursday, February 5, 2009

set icon for your website

add in head part of page

<link rel="shortcut icon" href="/img/favicon.png">

not only ico file, but also png image work well.

vi config

vi ~/.vimrc

set tabstop=4
set shiftwidth=4
set nu
syntax on
set smartindent
set incsearch
set vb t_vb=

In CentOS, vim doesn't alias to vi for root (see /etc/profile.d/vim.sh)
so root must use vim directly.

set CPU frequency in Linux

  • ls /sys/devices/system/cpu/cpu0/
  • cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_driver etc.
scaling_available_governors: ondemand userspace performance
scaling_available_frequencies
  • modprobe acpi-cpufreq
  • cat /proc/cpuinfo | grep 'cpu MHz'
  • cpufreq-selector
    -c, --cpu CPU Number
    -g, --governor Governor
    -f, --frequency Frequency in KHz
  • /usr/sbin/cpuspeed -m 2000000 -M 2000000 -s 7
/etc/sysconfig/cpuspeed, configuration file
/etc/init.d/cpuspeed, initialzation script
/usr/sbin/cpuspeed, cpuspeed program
  • cpufreq-set
cpufreq-set -d 2667000 -u 2667000 -c 0
  • cpufreq-info

linux networking command

  • print route table
netstat -r
  • add route table entry
route add -net 172.16.2.0 netmask 255.255.255.0 gw 172.16.2.2
  • restart network
/etc/init.d/networking restart

Build and using a new glibc

  1. export LD_LIBRARY_PATH=
  2. ../glibc-2.5/configure --prefix=/home/xxxx/nptl/install
  3. make
  4. make install
  5. link to new glibc
gcc test.c -L/home/xxxx/nptl/install/lib -I/home/xxxx/nptl/install/include -static -lpthread

  • modify glibc
some definition, include pthread_mutex_t etc.
glibc-2.5/nptl/sysdeps/unix/sysv/linux/i386/bits/pthreadtypes.h

some nptl function
glibc-2.5/nptl/pthread_mutex_lock.c
glibc-2.5/nptl/sysdeps/i386/pthread_spin_lock.c

cp glibc-2.5/nptl/sysdeps/unix/sysv/linux/i386/bits/pthreadtypes.h install/include/bits/pthreadtypes.h
cp build/nptl/libp* install/lib
rm build/libc.so

Assembly manual

Inline assembly for x86 in Linux

AT&T ASM Syntax

manual

Wednesday, February 4, 2009

set meta information for a page in Symfony

$response = $this->getResponse();
$response->setTitle($title);
$response->addMeta('keywords', $kws);
$response->addMeta('description', $desc);

get thread id

#include
#include
#include


int my_gettid()
{
int * p = (int*)pthread_self();
int pid = getpid();
assert(pid == p[19]);
return p[18];
}

int my_gettid(pthread_t thread)
{
int *p = (int*)thread;
return p[18];
}

void BusyWork(void* t)
{
int tid = my_gettid();
int pid = getpid();
printf("worker tid %d pid %d\n",tid,pid);
}

int main()
{
int tid = my_gettid();
int pid = getpid();
printf("main tid %d pid %d\n",tid,pid);

pthread_t thread;
pthread_create(&thread, NULL, BusyWork, NULL);

pthread_join(thread,NULL);
return 0;
}

pthread spin lock in glibc nptl

  • The Glibc I using is glibc-2.5, computer architecture is i386
  • type definition of pthread_spinlock_t
nptl/sysdeps/unix/sysv/linux/i386/bits/pthreadtypes.h
/* POSIX spinlock data type. */
typedef volatile int pthread_spinlock_t;

  • function pthread_spin_lock
nptl/sysdeps/i386/pthread_spin_lock.c
  • my test of spin lock
#include
#include

#define NUM_THREADS 8
long sum;
pthread_spinlock_t sumlock;

int my_sleep(int i)
{
int j,k,n=0;
for(;i>0;i--)
for(j=0;j<100;j++)
for(k=0;k<100;k++)
n++;
return n;
}

void *BusyWork(void *threadid)
{
int i,k;
long tid = (long)threadid;
for(i=0;i<10;i++){
k = my_sleep(3);
pthread_spin_lock (&sumlock);
printf("%d ", tid);
sum += k;
pthread_spin_unlock (&sumlock);
}
pthread_exit((void *) 0);
}

int main (int argc, char *argv[])
{
pthread_t thread[NUM_THREADS];
int rc, t;
void *status;
pthread_spin_init(&sumlock, NULL);
for(t=0; t
rc = pthread_create(&thread[t], NULL, BusyWork, (void*)t);
if (rc){
return 0;
}
}
for(t=0; t
rc = pthread_join(thread[t], &status);
if (rc){
printf("ERROR; return code from pthread_join() is %d\n", rc);
return 0;
}
}
printf("sum = %ld\n", sum);
pthread_spin_destroy(&sumlock);
pthread_exit(NULL);
}

Bind a thread/process to a CPU core

#include <sched.h>
  • Bind the invoke thread to a CPU core which id is p
void thread_bind(int p)
{
cpu_set_t mask;
__CPU_ZERO(&mask);
__CPU_SET(p, &mask);
sched_setaffinity(0, sizeof(mask), &mask);
}
  • Bind thread which id is tid to a CPU core p
void thread_bind(int p, int tid)
{
cpu_set_t mask;
__CPU_ZERO(&mask);
__CPU_SET(p, &mask);
sched_setaffinity(tid, sizeof(mask), &mask);
}

Reference
  1. Get thread id

Add a /proc/ file using Linux kernel module

Reference
1. Create a Linux Module
2. http://www.ibm.com/developerworks/linux/library/l-proc.html

vi command

1. full text replacement
:1,$s/tom/David/g
from first line to last line, replace tom with David

Search in Linux

grep -R xxxx .

Tuesday, February 3, 2009

Create a Linux module

1. vi simple-lmk.c
#include 

/* Defines the license for this LKM */
MODULE_LICENSE("GPL");

/* Init function called on module entry */
int my_module_init( void )
{
printk(KERN_INFO "my_module_init called. Module is now loaded.\n");
return 0;
}

/* Cleanup function called on module exit */
void my_module_cleanup( void )
{
printk(KERN_INFO "my_module_cleanup called. Module is now unloaded.\n");
return;
}

/* Declare entry and exit functions */
module_init( my_module_init );
module_exit( my_module_cleanup );

2. vi Makefile

obj-m += simple-lkm.o

3. build
make -C /usr/src/linux-`uname -r` SUBDIRS=$PWD modules

4. insert module
insmod simple-lkm.ko

5. check module
lsmod

6. check print message
dmesg | tail -5

7. remove module
rmmod simple-lkm

Connect VMware client from Linux host

I setup a Linux virtual machine on a Linux host, using NAT network.
When connect from virtual machine to host, it is successful. But there is a problem from host connect to virtual machine, the error message of ping is:
ping sendmsg operation not permitted

There need set an port mapping of NAT.
In host Linux, edit configure file /etc/vmware/vmnet8/nat/nat.conf
add one line "2222 = 192.168.27.128:22" under [incomingtcp]
192.168.27.128 is IP of virtual machine.

Then reboot

ssh -l root -p 2222 localhost
will telnet to virtual machine from host Linux.

Reference
1. http://www.vmware.com/support/ws55/doc/ws_net_nat_advanced.html

build and install a new Linux kernel

1. Down load a new linux kernel from http://www.kernel.org/
2. cp /boot/config-xxx .config
3. make
4. make modules_install install
5. reboot