Archive

Archive for May, 2010

我的emacs配置

May 26th, 2010 1 comment

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: 我的emacs配置

贴上来方便有用emacs的同学。配置部分是从网上淘来的,忘记出处了,见谅!

;;(keyboard-translate ?\C-h ?\C-?) 
(defalias 'yes-or-no-p 'y-or-n-p)

(setq time-stamp-active t)
(setq time-stamp-warn-inactive t)
(setq time-stamp-format "%:y-%02m-%02d %3a %02H:%02M:%02S")
(add-hook 'write-file-hooks 'time-stamp)

(global-set-key [f2] 'rgrep) 
(global-set-key [(f5)] 'eshell)
(global-set-key [C-f5] 'shell)
(global-set-key (kbd "M-3") 'capitalize-word)

(global-set-key [f6] 'find-file)
(global-set-key [C-Z] nil)
(global-set-key [C-f4] 'kill-this-buffer)

(global-set-key [(f4)] 'ibuffer)
(global-set-key [(f8)] 'other-window)

(require 'color-theme)
;;(color-theme-initialize)
(color-theme-dark-blue2)
(setq font-lock-verbose t)
;;(set-default-font "Bitstream Vera Sans Mono-12")
(global-font-lock-mode t)
(tool-bar-mode -1)
(setq column-number-mode t)
(setq display-battery-mode t)
(setq size-indication-mode t)
(setq default-major-mode 'outline-mode)
(add-hook 'outline-mode-hook 'turn-off-auto-fill)

(setq transient-mark-mode t)
(setq-default shell-cd-regexp nil)
(setq-default shell-pushd-regexp nil)
(setq-default shell-popd-regexp nil)
(setq inhibit-startup-message t)
(setq visible-bell t)
(setq mouse-yank-at-point t)
(setq kill-ring-max 100)
(setq default-fill-column 60)

(global-set-key (kbd "M-g") 'goto-line)
(global-set-key (kbd "M-o") 'ff-find-related-file)

(setq require-final-newline t)


(setq track-eol t)

(setq gnus-inhibit-startup-message t)

(setq enable-recursive-minibuffers t)
(setq auto-save-mode nil)
(setq Man-notify-method 'pushy)

(display-time-mode 1)
(setq display-time-24hr-format t)
(setq display-time-day-and-date t)
(setq display-time-use-mail-icon t)
(setq display-time-interval 10)

(tool-bar-mode -1)
(customize-set-variable 'scroll-bar-mode 'right)


(setq speedbar-show-unknown-files t)
(setq speedbar-update-flag nil)
(setq speedbar-use-images nil)
(setq speedbar-verbosity-level 0)
(global-set-key [f9] 'speedbar)

;; set paren mode
(show-paren-mode t)
(setq show-paren-style 'parentheses)
(mouse-avoidance-mode 'animate)

;; set the default username and email
(setq user-full-name "yufeng")
(setq user-mail-address "mryufeng@gmail.com")

;; set the title name
(setq frame-title-format "Emacs - %f")

;;-------------------------Shell 使用 ansi color-------------
(autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)

(require 'uniquify) 
(setq 
  uniquify-buffer-name-style 'post-forward
  uniquify-separator ":")

;;
(require 'ibuffer)
(global-set-key (kbd "C-x C-b") 'ibuffer)
(require 'ido)
(ido-mode t)

;;-------------------------让Emacs拥有tabs-------------------
(require 'tabbar)
(setq tabbar-buffer-groups-function
          (lambda ()
            (list "All")))

(tabbar-mode)
(global-set-key [(control shift tab)] 'tabbar-backward)
(global-set-key [(control tab)] 'tabbar-forward)
(global-set-key (kbd "M-1") 'tabbar-backward)
(global-set-key (kbd "M-2") 'tabbar-forward) 

;;(require 'session)
;;(add-hook 'after-init-hook 'session-initialize)
;;(load "desktop")
;;(desktop-load-default)
;;(desktop-read)
;;(setq desktop-save-mode t)

(require 'browse-kill-ring)
(global-set-key [(control c)(k)] 'browse-kill-ring)
(browse-kill-ring-default-keybindings)


;;erlang
;;(require 'erlang-eunit)
;;(load "erlang_appwiz" t nil)
(global-set-key [f3] 'erlang-next-error)
(global-set-key [C-f7] 'erlang-compile)
(global-set-key [f7] 'compile)
(global-set-key [f10] 'linum-mode)

;;distel
(add-to-list 'load-path "/usr/local/share/distel/elisp")
(require 'distel)
(distel-setup)
;; Some Erlang customizations
(add-hook 'erlang-mode-hook
	    (lambda ()
	          ;; when starting an Erlang shell in Emacs, default in the node name
`	          (setq inferior-erlang-machine-options '("-sname" "emacs"))
		      ;; add Erlang functions to an imenu menu
		      (imenu-add-to-menubar "imenu")))

;; A number of the erlang-extended-mode key bindings are useful in the shell too
(defconst distel-shell-keys
  '(("\C-\M-i"   erl-complete)
    ("\M-?"      erl-complete)
    ("\M-."      erl-find-source-under-point)
    ("\M-,"      erl-find-source-unwind) 
    ("\M-*"      erl-find-source-unwind) 
    )
  "Additional keys to bind when in Erlang shell.")

(add-hook 'erlang-shell-mode-hook
	    (lambda ()
	          ;; add some Distel bindings to the Erlang shell
	          (dolist (spec distel-shell-keys)
		          (define-key erlang-shell-mode-map (car spec) (cadr spec)))))


;wrangler
(add-to-list 'load-path "/usr/local/share/wrangler/elisp") 
(require 'wrangler) 


;;ecb
(require 'xcscope)
(require 'cedet) ;;load cedet
(require 'ecb) ;;load ecb

(setq ecb-tip-of-the-day nil
inhibit-startup-message t
ecb-auto-compatibility-check nil
ecb-version-check nil)

(global-set-key [f12] 'ecb-activate)
(global-set-key [C-f12] 'ecb-deactivate)
(global-set-key [f11] 'delete-other-windows)
(global-set-key [(meta return)] 'semantic-ia-complete-symbol-menu) 
(global-set-key (kbd "C-c e") 'ecb-goto-window-edit-last) 
(global-set-key (kbd "C-c s") 'ecb-goto-window-sources) 
(global-set-key (kbd "C-c d") 'ecb-goto-window-directories)
(global-set-key (kbd "C-c m") 'ecb-goto-window-methods)
(global-set-key (kbd "C-c h") 'ecb-goto-window-history)
(global-set-key (kbd "C-c r") 'ecb-redraw-layout)
(global-set-key (kbd "C-.") 'cscope-find-global-definition-no-prompting)
(global-set-key (kbd "C-,") 'cscope-pop-mark)
(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(ecb-options-version "2.32")
 '(safe-local-variable-values (quote ((erlang-indent-level . 2)))))
(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 )

;;msf abbrev
(add-to-list 'load-path "~/emacs")
;; ensure abbrev mode is always on
(setq-default abbrev-mode t)
;; do not bug me about saving my abbreviations
(setq save-abbrevs nil)
;; load up modes I use
(require 'cc-mode)
;;(require 'perl-mode)
;;(require 'cperl-mode)
;;(require 'sh-script)
;;(require 'shell)
;;(require 'lua-mode)    
;;(require 'erlang)
;; load up abbrevs for these modes
(require 'msf-abbrev)
(setq msf-abbrev-verbose t) ;; optional
(setq msf-abbrev-root "~/emacs/mode-abbrevs")
(global-set-key (kbd "C-c l") 'msf-abbrev-goto-root)
(global-set-key (kbd "C-c a") 'msf-abbrev-define-new-abbrev-this-mode)
(msf-abbrev-load)

;;go mode
(require 'go-mode-load)
(require 'tramp)

;;gud
(add-hook 'gdb-mode-hook '(lambda ()
                            (define-key c-mode-base-map [(f5)] 'gud-go)
                            (define-key c-mode-base-map [(f7)] 'gud-step)
                            (define-key c-mode-base-map [(f8)] 'gud-next)))


(defvar no-easy-keys-minor-mode-map (make-keymap) 
  "no-easy-keys-minor-mode keymap.")
(let ((f (lambda (m)
           `(lambda () (interactive) 
              (message (concat "No! use " ,m " instead."))))))
  (dolist (l '(("<left>" . "C-b") ("<right>" . "C-f") ("<up>" . "C-p")
               ("<down>" . "C-n")
               ("<C-left>" . "M-f") ("<C-right>" . "M-b") ("<C-up>" . "M-{")
               ("<C-down>" . "M-}")
               ("<M-left>" . "M-f") ("<M-right>" . "M-b") ("<M-up>" . "M-{")
               ("<M-down>" . "M-}")
               ("<delete>" . "C-d") ("<C-delete>" . "M-d")
               ("<M-delete>" . "M-d") ("<next>" . "C-v") ("<C-next>" . "M-x <")
               ("<prior>" . "M-v") ("<C-prior>" . "M-x >") 
               ("<home>" . "C-a") ("<C-home>" . "M->")
               ("<C-home>" . "M-<") ("<end>" . "C-e") ("<C-end>" . "M->")))
    (define-key no-easy-keys-minor-mode-map
      (read-kbd-macro (car l)) (funcall f (cdr l)))))
(define-minor-mode no-easy-keys-minor-mode
  "A minor mode that disables the arrow-keys, pg-up/down, delete
  and backspace."  t " no-easy-keys"
  'no-easy-keys-minor-mode-map :global t)
(no-easy-keys-minor-mode 1)



(require 'smex)
(smex-initialize)

(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "M-X") 'smex-major-mode-commands)

(require 'org-install)
(add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
(define-key global-map "\C-cl" 'org-store-link)
(define-key global-map "\C-ca" 'org-agenda)
(setq org-log-done t)

(setq transient-mark-mode t) ; highlight text selection
(setq delete-selection-mode t) ; delete seleted text when typing

; highlight current line
(require 'hl-line)
(global-hl-line-mode t)

Post Footer automatically generated by wp-posturl plugin for wordpress.

Categories: 杂七杂八 Tags: ,

R14A实现了EEP31,添加了binary模块

May 21st, 2010 Comments off

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: R14A实现了EEP31,添加了binary模块

Erlang的binary数据结构非常强大,而且偏向底层,在作网络程序的时候,很方便的能够和二进制协议对应起来。但是由于这个数据结构加入erlang语言的时间不是很长,相关的配套模块不是很多。 在binary的匹配,替换,修改就显的非常麻烦。 于是有了EEP31 。 R14A昨天已经实现了这个功能, 在stdlib下添加了个binary模块。 这个模块大部分功能是由BIF实现的, 同时充分考虑了CPU使用的公平性,源码大部分在erl_bif_binary.c下。 还添加了个gurad函数: binary_part进一步方便我们写匹配条件。

我们在源码里面发现了以下注释:
/*
* The native implementation functions for the module binary.
* Searching is implemented using aither Boyer-More or Aho-Corasick
* depending on number of searchstrings (BM if one, AC if more than one).
* Native implementation is mostly for efficiency, nothing
* (except binary:referenced_byte_size) really *needs* to be implemented
* in native code.
*/

这个模块兼顾了效率和方便性,使用起来就大大简化了代码的复杂度,有福气了。

Post Footer automatically generated by wp-posturl plugin for wordpress.

Categories: Erlang探索 Tags: , , , , ,

R14A添加新指令优化Ref消息的接收

May 19th, 2010 1 comment

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: R14A添加新指令优化Ref消息的接收

Erlang的惯用法之一就是在消息匹配的时候,如果需要唯一性,通常会通过make_ref搞个唯一的Ref来作为消息的一部分来匹配。这个惯用法用在gen_server:call或者demonitor这样的使用频度很高的函数里面。由于erlang的消息匹配是再消息队列里面挨个遍历来匹配,特别是消息队列特别长的时候,会有很大的性能瓶颈。于是新的优化出现了,以下是编译器beam_receive.erl里面的解释,写的很清楚:

%%%                                                                                                                                                                    
%%% In code such as:                                                                                                                                                   
%%%                                                                                                                                                                    
%%%    Ref = make_ref(),        %Or erlang:monitor(process, Pid)                                                                                                       
%%%      .                                                                                                                                                             
%%%      .                                                                                                                                                             
%%%      .                                                                                                                                                             
%%%    receive                                                                                                                                                         
%%%       {Ref,Reply} -> Reply                                                                                                                                         
%%%    end.                                                                                                                                                            
%%%                                                                                                                                                                    
%%% we know that none of the messages that exist in the message queue                                                                                                  
%%% before the call to make_ref/0 can be matched out in the receive                                                                                                    
%%% statement. Therefore we can avoid going through the entire message                                                                                                 
%%% queue if we introduce two new instructions (here written as                                                                                                        
%%% BIFs in pseudo-Erlang):                                                                                                                                            
%%%                                                                                                                                                                    
%%%    recv_mark(SomeUniqInteger),                                                                                                                                     
%%%    Ref = make_ref(),                                                                                                                                               
%%%      .                                                                                                                                                             
%%%      .                                                                                                                                                             
%%%      .                                                                                                                                                             
%%%    recv_set(SomeUniqInteger),                                                                                                                                      
%%%    receive                                                                                                                                                         
%%%       {Ref,Reply} -> Reply                                                                                                                                         
%%%    end.                                                                                                                                                            
%%%                                                                                                                                                                    
%%% The recv_mark/1 instruction will save the current position and                                                                                                     
%%% SomeUniqInteger in the process context. The recv_set                                                                                                               
%%% instruction will verify that SomeUniqInteger is still stored                                                                                                       
%%% in the process context. If it is, it will set the current pointer                                                                                                  
%%% for the message queue (the next message to be read out) to the                                                                                                     
%%% position that was saved by recv_mark/1.                                                                                                                            
%%%    
%%% The remove_message instruction must be modified toinvalidate                                                                                                      
%%% the information stored by the previous recv_mark/1, in case there                                                                                                  
%%% is another receive executed between the calls to recv_mark/1 and                                                                                                   
%%% recv_set/1.                                                                                                                                                        
%%%                                                                                                                                                                    
%%% We use a reference to a label (i.e. a position in the loaded code)                                                                                                 
%%% as the SomeUniqInteger.                                                                                                                                            
%%%                                                   

结论: 点滴优化成就高性能。

Post Footer automatically generated by wp-posturl plugin for wordpress.

非阻塞connect的一个细节

May 18th, 2010 4 comments

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: 非阻塞connect的一个细节

昨天听zhuzhaoyuan说的一个connect细节. 通常我们connect的时候都是非阻塞的, 在connect调用后把句柄挂到poll去, 等poll通知可写的时候, 我们就认为connect成功了. 但是在linux平台下实际上不一定成功, 具体的要用socket get_opt来检查下出错码来决定.

以下是从man 2 connnect摘抄的:

EINPROGRESS
The socket(2,7,n) is non-blocking and the connection cannot be com-
pleted immediately. It is possible to select(2,7,2 select_tut)(2) or poll(2) for
completion by selecting the socket(2,7,n) for writing. After select(2,7,2 select_tut)
indicates writability, use getsockopt(2) to read(2,n,1 builtins) the SO_ERROR
option at level SOL_SOCKET to determine whether connect com-
pleted successfully (SO_ERROR is zero) or unsuccessfully
(SO_ERROR is one of the usual error(8,n) codes listed here, explain-
ing the reason for the failure).

我们看下erlang的inet_drv是如何处理的.
./erts/emulator/drivers/common/inet_drv.c:

        {
            int error = 0;      /* Has to be initiated, we check it */
            unsigned int sz = sizeof(error); /* even if we get -1 */
            int code = sock_getopt(desc->inet.s, SOL_SOCKET, SO_ERROR,
                                   (void *)&error, &sz);

            if ((code < 0) || error) {
                desc->inet.state = TCP_STATE_BOUND;  /* restore state */
                ret = async_error(INETP(desc), error);
                goto done;
            }
        }

结论: 细节决定品质.

Post Footer automatically generated by wp-posturl plugin for wordpress.

中国地图(俺去过的地方)

May 12th, 2010 1 comment

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: 中国地图(俺去过的地方)


create your own China map

持续更新!

Post Footer automatically generated by wp-posturl plugin for wordpress.

Categories: 生活 Tags:

How to Build a Debug Enabled Erlang RunTime System

May 7th, 2010 1 comment

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: How to Build a Debug Enabled Erlang RunTime System

很多朋友在问如何调试Erlang的驱动代码等等,其实otp源码下的INSTALL.md写的很清楚, 摘抄下:
How to Build a Debug Enabled Erlang RunTime System
————————————————–

After completing all the normal building steps described above a debug
enabled runtime system can be built. To do this you have to change
directory to `$ERL_TOP/erts/emulator`.
注:一定要注意这句话, 假设你现在在otp源码目录下,正常编译好了, export ERL_TOP=`pwd` 然后进入erts/emulator目录下

In this directory execute:

$ make debug FLAVOR=$FLAVOR

where `$FLAVOR` is either `plain` or `smp`. The flavor options will
produce a beam.debug and beam.smp.debug executable respectively. The
files are installed along side with the normal (opt) versions `beam.smp`
and `beam`.

To start the debug enabled runtime system execute:

$ $ERL_TOP/bin/cerl -debug

The debug enabled runtime system features lock violation checking,
assert checking and various sanity checks to help a developer ensure
correctness. Some of these features can be enabled on a normal beam
using appropriate configure options.

There are other types of runtime systems that can be built as well
using the similar steps just described.

$ make $TYPE FLAVOR=$FLAVOR

where `$TYPE` is `opt`, `gcov`, `gprof`, `debug`, `valgrind`, or `lcnt`.
These different beam types are useful for debugging and profiling
purposes.

祝玩的开心!

Post Footer automatically generated by wp-posturl plugin for wordpress.

Categories: Erlang探索 Tags: , ,

erts_modified_timings是如何起作用的

May 3rd, 2010 Comments off

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: erts_modified_timings是如何起作用的

我们先看下man erl

+T Level:

Enables modified timing and sets the modified timing level. Currently valid range is 0-9. The timing of the runtime system will
change. A high level usually means a greater change than a low level. Changing the timing can be very useful for finding timing
related bugs.

Currently, modified timing affects the following:

Process spawning:
A process calling spawn, spawn_link, spawn_monitor, or spawn_opt will be scheduled out immediately after completing the
call. When higher modified timing levels are used, the caller will also sleep for a while after being scheduled out.

Context reductions:
The amount of reductions a process is a allowed to use before being scheduled out is increased or reduced.

Input reductions:
The amount of reductions performed before checking I/O is increased or reduced.

NOTE: Performance will suffer when modified timing is enabled. This flag is only intended for testing and debugging. Also note
that return_to and return_from trace messages will be lost when tracing on the spawn BIFs. This flag may be removed or changed
at any time without prior notice.

我们可以知道这个选项是用于修改VM的进程和IO调度的时间,以及延迟spawn的执行时间, 使得和时间相关的问题,容易得到暴露,达到发现问题的目的。 但是Level是如何对于参数的呢?下面这个表格能回答问题了.
erts/emulator/beam/erl_init.c

 132ErtsModifiedTimings erts_modified_timings[] = {                                                    
 133    /* 0 */     {make_small(0), CONTEXT_REDS, INPUT_REDUCTIONS},                            
 134    /* 1 */     {make_small(0), 2*CONTEXT_REDS, 2*INPUT_REDUCTIONS},                               
 135    /* 2 */     {make_small(0), CONTEXT_REDS/2, INPUT_REDUCTIONS/2},                               
 136    /* 3 */     {make_small(0), 3*CONTEXT_REDS, 3*INPUT_REDUCTIONS},                               
 137    /* 4 */     {make_small(0), CONTEXT_REDS/3, 3*INPUT_REDUCTIONS},                               
 138    /* 5 */     {make_small(0), 4*CONTEXT_REDS, INPUT_REDUCTIONS/2},                               
 139    /* 6 */     {make_small(1), CONTEXT_REDS/4, 2*INPUT_REDUCTIONS},                               
 140    /* 7 */     {make_small(1), 5*CONTEXT_REDS, INPUT_REDUCTIONS/3},                               
 141    /* 8 */     {make_small(10), CONTEXT_REDS/5, 3*INPUT_REDUCTIONS},                              
 142    /* 9 */     {make_small(10), 6*CONTEXT_REDS, INPUT_REDUCTIONS/4}                               
 143};                                                                                                 
 

注意延时是以毫秒为单位的。

比如说: erl +T 8 那么spawn延时10ms, IO处理时间加大到3倍, 有利于快速处理IO事件。

Post Footer automatically generated by wp-posturl plugin for wordpress.

Categories: Erlang探索 Tags: , ,