最近在做OpenStack Cinder driver的性能調試, 之前一直是通過在driver里面加入decorator,完成driver各個接口的執行時間的統計。
其實在openstack,已經在孵化一個叫osprofiler的project,然后這個可以通過與OpenStack Ceilometer的集成,可以輕松完成性能數據的統計,大幅的節省性能調優的時間.
- osprofiler原理:
通過在OpenStack不同Component之間使用osprofiler的trace,記錄所有的wsgi,rpc,driver各個接口的開始和結束時間,然后通過rpc message把記錄到的數據發送到Ceilometer數據庫進行存儲。
這樣用戶可以在執行完OpenStack的操作后,通過osprofiler的CLI接口,以html或者JSON的格式可視化的顯示出各個接口的執行順序和時間,從而發現一個call stack的瓶頸。
更多關於osprofier可以參見https://github.com/stackforge/osprofiler
[NOTE]: 有網友反映和本人自己的實驗,在最新的master branch上,不能正確產生正確的osprofiler數據,error如下:
[待增加]
解決方案是使用kilo版本:
cd ~/devstack # 保存當前的change git stash git checkout stable/kilo # 重新應用change git stash pop # 其他配置保持不變 # 讓后在./stack.sh ./stack.sh
#升級python-cinderclient,安裝python-ceilometerclient
sudo pip install python-cinderclient --upgrade
sudo pip install python-ceilometerclient
- 基本的使用:
from osprofiler import profiler # 使用前,一定要init,否則不會用任何的數據記錄 profiler.init("SECRET_HMAC_KEY", base_id='sadfsdafasdfasdfas', parent_id='dsafafasdfsadf') def some_func(): profiler.start("point_name", {"any_key": "with_any_value"}) # your code print "I am between some_func" profiler.stop({"any_info_about_point": "in_this_dict"}) @profiler.trace("point_name", info={"any_info_about_point": "in_this_dict"}, hide_args=False) def some_func2(*args, **kwargs): # If you need to hide args in profile info, put hide_args=True print "Hello, osprofiler" pass def some_func3(): with profiler.Trace("point_name", info={"any_key": "with_any_value"}): # some code here pass @profiler.trace_cls("point_name", info={}, hide_args=False, trace_private=False) class TracedClass(object): def traced_method(self): print "Trace me" pass def _traced_only_if_trace_private_true(self): pass # 把所有的記錄寫入到json文件里面 def send_info_to_file_collector(info, context=None): with open("traces", "a") as f: f.write(json.dumps(info)) notifier.set(send_info_to_file_collector) # 下面的函數調用都會被一一記錄 some_func() some_func2(test='asdfasdf', adf=313) trace = TracedClass() trace.traced_method()
然后,你在當前目錄的 traces 文件問發現很多的log,有個問題數據的可讀性比較差,那么OpenStack是怎么解決的呢?
答案是配合使用 Ceilometer. 關於Ceilometer,參考它的框架,可以幫助理解http://docs.openstack.org/developer/ceilometer/architecture.html#high-level-architecture
下面以lvm 的cinder driver為例,說明如何配置Cinder,osprofiler以及Ceilometer的集成,,
- 首先,要setup devstack,下面是devstack的local.conf的配置: (如果你是首次使用devstack,請移步http://www.cnblogs.com/sting2me/p/4140275.html)
(注意,我enable了Ceilometer和Neutron的所有組件,在使用這個文件時,要把HOST_IP, SERVICE_HOST改為本機的IP
[[local|localrc]] HOST_IP=192.168.14.128 SERVICE_HOST=192.168.14.128 ADMIN_PASSWORD=welcome DATABASE_PASSWORD=$ADMIN_PASSWORD RABBIT_PASSWORD=$ADMIN_PASSWORD SERVICE_PASSWORD=$ADMIN_PASSWORD SERVICE_TOKEN=$ADMIN_PASSWORD DEST=/opt/stack LOGFILE=$DEST/logs/stack.sh.log SCREEN_LOGDIR=$DEST/logs/screen OFFLINE=False RECLONE=False LOG_COLOR=False disable_service horizon enable_service q-svc enable_service q-agt enable_service q-dhcp enable_service q-l3 enable_service q-meta enable_service neutron # Enable the ceilometer metering services enable_service ceilometer-acompute ceilometer-acentral ceilometer-anotification ceilometer-collector # Enable the ceilometer alarming services enable_service ceilometer-alarm-evaluator,ceilometer-alarm-notifier # Enable the ceilometer api services enable_service ceilometer-api
# 這個profiler一定要加,是cinder的性能信息記錄到Ceilometer的關鍵 CEILOMETER_NOTIFICATION_TOPICS=notifications,profiler disable_service n-net disable_service tempest disable_service h-eng,h-api,h-api-cfn,h-api-cw PHYSICAL_NETWORK=physnet1 FIXED_RANGE=192.168.106.0/24 FIXED_NETWORK_SIZE=32 NETWORK_GATEWAY=192.168.106.1 [[post-config|$CINDER_CONF]] [profiler] profiler_enabled = True trace_sqlalchemy = False [[post-config|/$Q_PLUGIN_CONF_FILE]] [ml2] tenant_network_types = vlan [ml2_type_vlan] network_vlan_ranges = physnet1:100:110 [ovs] bridge_mappings = physnet1:br-eth1 enable_tunneling = False
然后就是執行 ./stack.sh
- 產生與收集Cinder操作的性能數據:
peter@ubuntu:~/devstack$ cinder --profile SECRET_KEY create --name peter 1 +---------------------------------------+--------------------------------------+ | Property | Value | +---------------------------------------+--------------------------------------+ | attachments | [] | | availability_zone | nova | | bootable | false | | consistencygroup_id | None | | created_at | 2015-04-04T14:58:51.000000 | | description | None | | encrypted | False | | id | 28857983-3240-445d-a60b-3b91295c31e8 | | metadata | {} | | multiattach | False | | name | peter | | os-vol-host-attr:host | None | | os-vol-mig-status-attr:migstat | None | | os-vol-mig-status-attr:name_id | None | | os-vol-tenant-attr:tenant_id | ade7584debc54964b4fef737e56e062d | | os-volume-replication:driver_data | None | | os-volume-replication:extended_status | None | | replication_status | disabled | | size | 1 | | snapshot_id | None | | source_volid | None | | status | creating | | user_id | 56aac792735046dea02e12e85e0d1a03 | | volume_type | lvmdriver-1 | +---------------------------------------+--------------------------------------+ Trace ID: aa4903cc-fd0c-42ef-96f1-bd1c5a1740f1 To display trace use next command: osprofiler trace show --html aa4903cc-fd0c-42ef-96f1-bd1c5a1740f1
- 導出性能測試的數據為HTML格式
osprofiler trace show --html aa4903cc-fd0c-42ef-96f1-bd1c5a1740f1 --out test.html
html內容如下(需要翻牆,這個頁面需要訪問google.com ^_^)
- 查看各個接口的執行時間,下圖給出了每個被統計的接口在不同的service之間的執行時間
- 查看接口的參數,可以點擊函數,查看具體的參數
上面的view可以很輕松的實現對openstack各個接口調用執行時間的統計,可視化的顯示了特定操作的性能瓶頸在哪里。
- Cinder driver的各個接口的性能統計:
上面的工作,還有一個問題沒有解決,如果你的driver其實有多個層級的class(如driver.create_volume->AnotherClass.def1->AnotherClass.def2->AnotherClass.defn),那么我只知道入口函數driver.create_volume的執行時間,並不知道在AnotherClass內部各個接口的執行時間(def1多少時間,def2多少時間。這個就要稍微改下lvm driver的代碼了,可以為lvm driver的所有class 加上如下的decorator:
@profiler.trace_cls("AnotherClass", info={}, hide_args=False, trace_private=True) class AnotherClass(object): def def1: pass def def2: pass
這樣更改代碼后,然后再重啟cinder service,你會得到更詳細的數據
參考文章及深入閱讀: