This article is meant to be an easy and relatively safe way to enhance mysql performance. It is not meant to be a complete guide to tuning MySQL. Fully optimizing MySQL takes both time and effort since every application has different requirements. The Debian MySQL packages ship with very conservative memory usage settings and the same is probably true for other Linux distributions and the Windows binaries. If you loosen these settings up a little, MySQL will perform much faster under ordinary circumstances.
The items are ordered on impact, high to low. Feedback and hints will be greatly appreciated.
Key Buffer
The key buffer holds the indexes of tables in memory and a bigger key buffer results in faster row lookups. Adjust according to your own needs. Bigger is better, but prevent swapping at all costs. A good rule of thumb seems to be to use 1/4 of system memory.
key_buffer = 128M
Query Cache
This is where the magic happens. Well, not magic really, just plain old caching. Keeping the result of queries in memory until they are invalidated by additional writes enhances performance by magnitudes. The query_cache_size, as the name suggests, is the total size of memory available to query caching. The value query_cache_limit is the maximum number of kilobytes one query may be in order to be cached. Setting this value too high might prevent a lot of smaller queries to be cached. Setting it too low will result in bigger queries to never be cached, and the smaller queries not being able to completely fill the cache size, which would be a waste of resources. Adjust according to your own needs and memory available:
query_cache_size = 128MB
query_cache_limit = 4MB
Table Cache
An important variable if your application accesses many tables. It is the number of tables a thread can keep open at the same time. A value of 512 should do no harm.
table_cache = 512
Sort Buffers
sort_buffer_size (the variable previously known as sort_buffer), used for grouping and sorting and is a per-thread buffer. If the buffer can not hold the data to be sorted, a sort is performed on disk. Watch out for making this too large as the buffer is allocated for every thread that needs sorting and with many sorts it can easily consume all your memory.
sort_buffer_size = 32M
myisam_sort_buffer_size = 32M
The InnoDB Engine
Most people do not use the InnoDB engine in MySQL and use MyISAM instead. Since MySQL reserves memory for this engine, you are better off without it. If you need InnoDB, you can find more on its settings in the official MySQL docs.
Add `skip-innodb’ to my.cnf to disable the engine.
Binary Logging
MySQL has a few powerful features. Replicating data changes to a second server is one of them. MySQL keeps a log file of data changes which is used for this purpose. If you do not use replication or use the file as incremental backup, you can disable it. This will save you expensive disk write actions for every change to your data. For applications that have a lot of frequently updated data, this can be quite a performance boost. According to the official docs, this will generally result in just a 1% boost but it’s an easy gain if you do not need the log. Read more about the binary log here. Comment the following line:
log-bin = /var/log/mysql/mysql-bin.log
Temporary Tables
Temporary tables are used for sorting and grouping. The buffer is created on demand so watch out for setting this too high here as well. If the buffer cannot accomodate the data, a temp file is used on disk instead.
tmp_table_size = 64MB
Delayed Writing
This setting can greatly improve writing or updating data to a table. Instead of directly committing data to the disk, MySQL queues writes and returns write queries immediately. Be very very careful with this, because this also means that in case of a power failure or crash, you lose data. You can use this for logging if you don’t mind losing a couple of rows in case of a crash.
delay_key_write = 1
Connection Timeout
This is a little tweak that determines the closing of sleeping connections. The default is one hour and is often too long for practical purposes. I often set this at one minute instead (60).
wait_timeout = 60
The above settings are just to make mysql a little faster in general. You can get much better speed improvements by optimizing the database itself. Setting the correct indexes on tables can be a life-saver.
2009年3月17日星期二
InnoDB和MyISAM的差别(mysql事务处理)
InnoDB 和MyISAM是在使用MySQL最常用的两个表类型,各有优缺点,视具体应用而定。基本的差别为:MyISAM类型不支持事务处理等高级处理,而 InnoDB类型支持。MyISAM类型的表强调的是性能,其执行数度比InnoDB类型更快,但是不提供事务支持,而InnoDB提供事务支持已经外部键等高级数据库功能。
MyIASM是IASM表的新版本,有如下扩展:
二进制层次的可移植性。
NULL列索引。
对变长行比ISAM表有更少的碎片。
支持大文件。
更好的索引压缩。
更好的键吗统计分布。
更好和更快的auto_increment处理。
1.MySQL最大的优势在于MyISAM引擎下的简单SELECT,INSERT和UPDATE快速操作
2.MyISAM类型的数据文件可以在不同操作系统中COPY,这点很重要,布署的时候方便点。
以下是一些细节和具体实现的差别:
1.InnoDB不支持FULLTEXT类型的索引。
2.InnoDB 中不保存表的具体行数,也就是说,执行select count(*) from table时,InnoDB要扫描一遍整个表来计算有多少行,但是MyISAM只要简单的读出保存好的行数即可。注意的是,当count(*)语句包含 where条件时,两种表的操作是一样的。
3.对于AUTO_INCREMENT类型的字段,InnoDB中必须包含只有该字段的索引,但是在MyISAM表中,可以和其他字段一起建立联合索引。
4.DELETE FROM table时,InnoDB不会重新建立表,而是一行一行的删除。
5.LOAD TABLE FROM MASTER操作对InnoDB是不起作用的,解决方法是首先把InnoDB表改成MyISAM表,导入数据后再改成InnoDB表,但是对于使用的额外的InnoDB特性(例如外键)的表不适用。
另外,InnoDB表的行锁也不是绝对的,如果在执行一个SQL语句时MySQL不能确定要扫描的范围,InnoDB表同样会锁全表,例如update table set num=1 where name like “%aaa%”
以暂对存储引擎的认识,觉得 InnoDB 支持外键,在数据量可以用“庞大”来形容时,在有良好的 INDEX 的基础上,InnoDB 的查询速度应该比 MyISAM 要快。
在 Falcon 有稳定版本前,我想 MyISAM 是一个可用的选择方案。
任何一种表都不是万能的,只用恰当的针对业务类型来选择合适的表类型,才能最大的发挥MySQL的性能优势。
MyIASM是IASM表的新版本,有如下扩展:
二进制层次的可移植性。
NULL列索引。
对变长行比ISAM表有更少的碎片。
支持大文件。
更好的索引压缩。
更好的键吗统计分布。
更好和更快的auto_increment处理。
1.MySQL最大的优势在于MyISAM引擎下的简单SELECT,INSERT和UPDATE快速操作
2.MyISAM类型的数据文件可以在不同操作系统中COPY,这点很重要,布署的时候方便点。
以下是一些细节和具体实现的差别:
1.InnoDB不支持FULLTEXT类型的索引。
2.InnoDB 中不保存表的具体行数,也就是说,执行select count(*) from table时,InnoDB要扫描一遍整个表来计算有多少行,但是MyISAM只要简单的读出保存好的行数即可。注意的是,当count(*)语句包含 where条件时,两种表的操作是一样的。
3.对于AUTO_INCREMENT类型的字段,InnoDB中必须包含只有该字段的索引,但是在MyISAM表中,可以和其他字段一起建立联合索引。
4.DELETE FROM table时,InnoDB不会重新建立表,而是一行一行的删除。
5.LOAD TABLE FROM MASTER操作对InnoDB是不起作用的,解决方法是首先把InnoDB表改成MyISAM表,导入数据后再改成InnoDB表,但是对于使用的额外的InnoDB特性(例如外键)的表不适用。
另外,InnoDB表的行锁也不是绝对的,如果在执行一个SQL语句时MySQL不能确定要扫描的范围,InnoDB表同样会锁全表,例如update table set num=1 where name like “%aaa%”
以暂对存储引擎的认识,觉得 InnoDB 支持外键,在数据量可以用“庞大”来形容时,在有良好的 INDEX 的基础上,InnoDB 的查询速度应该比 MyISAM 要快。
在 Falcon 有稳定版本前,我想 MyISAM 是一个可用的选择方案。
任何一种表都不是万能的,只用恰当的针对业务类型来选择合适的表类型,才能最大的发挥MySQL的性能优势。
MySQL中MyISAM引擎与InnoDB引擎性能简单测试
[硬件配置]
CPU : AMD2500+ (1.8G)
内存: 1G/现代
硬盘: 80G/IDE
[软件配置]
OS : Windows XP SP2
SE : PHP5.2.1
DB : MySQL5.0.37
Web: IIS6
[MySQL表结构]
CREATE TABLE `myisam` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) default NULL,
`content` text,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
CREATE TABLE `innodb` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) default NULL,
`content` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
[数据内容]
$name = "heiyeluren";
$content = "MySQL支持数个存储引擎作为对不同表的类型的处理器。MySQL存储引擎包括处理事务安全表的引擎和处理非事务安全表的引擎:· MyISAM管理非事务表。它提供高速存储和检索,以及全文搜索能力。MyISAM在所有MySQL配置里被支持,它是默认的存储引擎,除非你配置MySQL默认使用另外一个引擎。 ·MEMORY存储引擎提供“内存中”表。MERGE存储引擎允许集合将被处理同样的MyISAM表作为一个单独的表。就像MyISAM一样,MEMORY和MERGE存储引擎处理非事务表,这两个引擎也都被默认包含在MySQL中。 释:MEMORY存储引擎正式地被确定为HEAP引擎。· InnoDB和BDB存储引擎提供事务安全表。BDB被包含在为支持它的操作系统发布的MySQL-Max二进制分发版里。InnoDB也默认被包括在所有MySQL 5.1二进制分发版里,你可以按照喜好通过配置MySQL来允许或禁止任一引擎。·EXAMPLE存储引擎是一个“存根”引擎,它不做什么。你可以用这个引擎创建表,但没有数据被存储于其中或从其中检索。这个引擎的目的是服务,在MySQL源代码中的一个例子,它演示说明如何开始编写新存储引擎。同样,它的主要兴趣是对开发者。";
[插入数据-1] (innodb_flush_log_at_trx_commit=1)
MyISAM 1W:3/s
InnoDB 1W:219/s
MyISAM 10W:29/s
InnoDB 10W:2092/s
MyISAM 100W:287/s
InnoDB 100W:没敢测试
[插入数据-2] (innodb_flush_log_at_trx_commit=0)
MyISAM 1W:3/s
InnoDB 1W:3/s
MyISAM 10W:30/s
InnoDB 10W:29/s
MyISAM 100W:273/s
InnoDB 100W:423/s
[插入数据3] (innodb_buffer_pool_size=1024M)
InnoDB 1W:3/s
InnoDB 10W:33/s
InnoDB 100W:607/s
[插入数据4] (innodb_buffer_pool_size=256M, innodb_flush_log_at_trx_commit=1, set autocommit=0)
InnoDB 1W:3/s
InnoDB 10W:26/s
InnoDB 100W:379/s
[MySQL 配置文件] (缺省配置)
# MySQL Server Instance Configuration File
[client]
port=3306
[mysql]
default-character-set=gbk
[mysqld]
port=3306
basedir="C:/mysql50/"
datadir="C:/mysql50/Data/"
default-character-set=gbk
default-storage-engine=INNODB
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
max_connections=100
query_cache_size=0
table_cache=256
tmp_table_size=50M
thread_cache_size=8
myisam_max_sort_file_size=100G
myisam_max_extra_sort_file_size=100G
myisam_sort_buffer_size=100M
key_buffer_size=82M
read_buffer_size=64K
read_rnd_buffer_size=256K
sort_buffer_size=256K
innodb_additional_mem_pool_size=4M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=2M
innodb_buffer_pool_size=159M
innodb_log_file_size=80M
innodb_thread_concurrency=8
【总结】
可以看出在MySQL 5.0里面,MyISAM和InnoDB存储引擎性能差别并不是很大,针对InnoDB来说,影响性能的主要是 innodb_flush_log_at_trx_commit 这个选项,如果设置为1的话,那么每次插入数据的时候都会自动提交,导致性能急剧下降,应该是跟刷新日志有关系,设置为0效率能够看到明显提升,当然,同样你可以SQL中提交“SET AUTOCOMMIT = 0”来设置达到好的性能。另外,还听说通过设置innodb_buffer_pool_size能够提升InnoDB的性能,但是我测试发现没有特别明显的提升。
基本上我们可以考虑使用InnoDB来替代我们的MyISAM引擎了,因为InnoDB自身很多良好的特点,比如事务支持、存储过程、视图、行级锁定等等,在并发很多的情况下,相信InnoDB的表现肯定要比MyISAM强很多,当然,相应的在my.cnf中的配置也是比较关键的,良好的配置,能够有效的加速你的应用。
如果不是很复杂的Web应用,非关键应用,还是可以继续考虑MyISAM的,这个具体情况可以自己斟酌。
参考URL:
http://dev.mysql.com/doc/refman/5.1/zh/index.html
http://dev.mysql.com/doc/refman/5.1/zh/storage-engines.html#innodb
CPU : AMD2500+ (1.8G)
内存: 1G/现代
硬盘: 80G/IDE
[软件配置]
OS : Windows XP SP2
SE : PHP5.2.1
DB : MySQL5.0.37
Web: IIS6
[MySQL表结构]
CREATE TABLE `myisam` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) default NULL,
`content` text,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
CREATE TABLE `innodb` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) default NULL,
`content` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
[数据内容]
$name = "heiyeluren";
$content = "MySQL支持数个存储引擎作为对不同表的类型的处理器。MySQL存储引擎包括处理事务安全表的引擎和处理非事务安全表的引擎:· MyISAM管理非事务表。它提供高速存储和检索,以及全文搜索能力。MyISAM在所有MySQL配置里被支持,它是默认的存储引擎,除非你配置MySQL默认使用另外一个引擎。 ·MEMORY存储引擎提供“内存中”表。MERGE存储引擎允许集合将被处理同样的MyISAM表作为一个单独的表。就像MyISAM一样,MEMORY和MERGE存储引擎处理非事务表,这两个引擎也都被默认包含在MySQL中。 释:MEMORY存储引擎正式地被确定为HEAP引擎。· InnoDB和BDB存储引擎提供事务安全表。BDB被包含在为支持它的操作系统发布的MySQL-Max二进制分发版里。InnoDB也默认被包括在所有MySQL 5.1二进制分发版里,你可以按照喜好通过配置MySQL来允许或禁止任一引擎。·EXAMPLE存储引擎是一个“存根”引擎,它不做什么。你可以用这个引擎创建表,但没有数据被存储于其中或从其中检索。这个引擎的目的是服务,在MySQL源代码中的一个例子,它演示说明如何开始编写新存储引擎。同样,它的主要兴趣是对开发者。";
[插入数据-1] (innodb_flush_log_at_trx_commit=1)
MyISAM 1W:3/s
InnoDB 1W:219/s
MyISAM 10W:29/s
InnoDB 10W:2092/s
MyISAM 100W:287/s
InnoDB 100W:没敢测试
[插入数据-2] (innodb_flush_log_at_trx_commit=0)
MyISAM 1W:3/s
InnoDB 1W:3/s
MyISAM 10W:30/s
InnoDB 10W:29/s
MyISAM 100W:273/s
InnoDB 100W:423/s
[插入数据3] (innodb_buffer_pool_size=1024M)
InnoDB 1W:3/s
InnoDB 10W:33/s
InnoDB 100W:607/s
[插入数据4] (innodb_buffer_pool_size=256M, innodb_flush_log_at_trx_commit=1, set autocommit=0)
InnoDB 1W:3/s
InnoDB 10W:26/s
InnoDB 100W:379/s
[MySQL 配置文件] (缺省配置)
# MySQL Server Instance Configuration File
[client]
port=3306
[mysql]
default-character-set=gbk
[mysqld]
port=3306
basedir="C:/mysql50/"
datadir="C:/mysql50/Data/"
default-character-set=gbk
default-storage-engine=INNODB
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
max_connections=100
query_cache_size=0
table_cache=256
tmp_table_size=50M
thread_cache_size=8
myisam_max_sort_file_size=100G
myisam_max_extra_sort_file_size=100G
myisam_sort_buffer_size=100M
key_buffer_size=82M
read_buffer_size=64K
read_rnd_buffer_size=256K
sort_buffer_size=256K
innodb_additional_mem_pool_size=4M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=2M
innodb_buffer_pool_size=159M
innodb_log_file_size=80M
innodb_thread_concurrency=8
【总结】
可以看出在MySQL 5.0里面,MyISAM和InnoDB存储引擎性能差别并不是很大,针对InnoDB来说,影响性能的主要是 innodb_flush_log_at_trx_commit 这个选项,如果设置为1的话,那么每次插入数据的时候都会自动提交,导致性能急剧下降,应该是跟刷新日志有关系,设置为0效率能够看到明显提升,当然,同样你可以SQL中提交“SET AUTOCOMMIT = 0”来设置达到好的性能。另外,还听说通过设置innodb_buffer_pool_size能够提升InnoDB的性能,但是我测试发现没有特别明显的提升。
基本上我们可以考虑使用InnoDB来替代我们的MyISAM引擎了,因为InnoDB自身很多良好的特点,比如事务支持、存储过程、视图、行级锁定等等,在并发很多的情况下,相信InnoDB的表现肯定要比MyISAM强很多,当然,相应的在my.cnf中的配置也是比较关键的,良好的配置,能够有效的加速你的应用。
如果不是很复杂的Web应用,非关键应用,还是可以继续考虑MyISAM的,这个具体情况可以自己斟酌。
参考URL:
http://dev.mysql.com/doc/refman/5.1/zh/index.html
http://dev.mysql.com/doc/refman/5.1/zh/storage-engines.html#innodb
2009年3月9日星期一
Top10SQLPerformanceTips
Interactive session from MySQL Camp I:
Specific Query Performance Tips (see also database design tips for tips on indexes):
1. Use EXPLAIN to profile the query execution plan
2. Use Slow Query Log (always have it on!)
3. Don't use DISTINCT when you have or could use GROUP BY
4. Insert performance
1. Batch INSERT and REPLACE
2. Use LOAD DATA instead of INSERT
5. LIMIT m,n may not be as fast as it sounds
6. Don't use ORDER BY RAND() if you have > ~2K records
7. Use SQL_NO_CACHE when you are SELECTing frequently updated data or large sets of data
8. Avoid wildcards at the start of LIKE queries
9. Avoid correlated subqueries and in select and where clause (try to avoid in)
10. No calculated comparisons -- isolate indexed columns
11. ORDER BY and LIMIT work best with equalities and covered indexes
12. Separate text/blobs from metadata, don't put text/blobs in results if you don't need them
13. Derived tables (subqueries in the FROM clause) can be useful for retrieving BLOBs without sorting them. (Self-join can speed up a query if 1st part finds the IDs and uses then to fetch the rest)
14. ALTER TABLE...ORDER BY can take data sorted chronologically and re-order it by a different field -- this can make queries on that field run faster (maybe this goes in indexing?)
15. Know when to split a complex query and join smaller ones
16. Delete small amounts at a time if you can
17. Make similar queries consistent so cache is used
18. Have good SQL query standards
19. Don't use deprecated features
20. Turning OR on multiple index fields (<5.0) into UNION may speed things up (with LIMIT), after 5.0 the index_merge should pick stuff up.
21. Don't use COUNT * on Innodb tables for every search, do it a few times and/or summary tables, or if you need it for the total # of rows, use SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS()
22. Use INSERT ... ON DUPLICATE KEY update (INSERT IGNORE) to avoid having to SELECT
23. use groupwise maximum instead of subqueries
Scaling Performance Tips:
1. Use benchmarking
2. isolate workloads don't let administrative work interfere with customer performance. (ie backups)
3. Debugging sucks, testing rocks!
4. As your data grows, indexing may change (cardinality and selectivity change). Structuring may want to change. Make your schema as modular as your code. Make your code able to scale. Plan and embrace change, and get developers to do the same.
Network Performance Tips:
1. Minimize traffic by fetching only what you need.
1. Paging/chunked data retrieval to limit
2. Don't use SELECT *
3. Be wary of lots of small quick queries if a longer query can be more efficient
2. Use multi_query if appropriate to reduce round-trips
3. Use stored procedures to avoid bandwidth wastage
OS Performance Tips:
1. Use proper data partitions
1. For Cluster. Start thinking about Cluster *before* you need them
2. Keep the database host as clean as possible. Do you really need a windowing system on that server?
3. Utilize the strengths of the OS
4. pare down cron scripts
5. create a test environment
6. source control schema and config files
7. for LVM innodb backups, restore to a different instance of MySQL so Innodb can roll forward
8. partition appropriately
9. partition your database when you have real data -- do not assume you know your dataset until you have real data
MySQL Server Overall Tips:
1. innodb_flush_commit=0 can help slave lag
2. Optimize for data types, use consistent data types. Use PROCEDURE ANALYSE() to help determine the smallest data type for your needs.
3. use optimistic locking, not pessimistic locking. try to use shared lock, not exclusive lock. share mode vs. FOR UPDATE
4. if you can, compress text/blobs
5. compress static data
6. don't back up static data as often
7. enable and increase the query and buffer caches if appropriate
8. config params -- http://docs.cellblue.nl/2007/03/17/easy-mysql-performance-tweaks/ is a good reference
9. Config variables & tips:
1. use one of the supplied config files
2. key_buffer, unix cache (leave some RAM free), per-connection variables, innodb memory variables
3. be aware of global vs. per-connection variables
4. check SHOW STATUS and SHOW VARIABLES (GLOBAL|SESSION in 5.0 and up)
5. be aware of swapping esp. with Linux, "swappiness" (bypass OS filecache for innodb data files, innodb_flush_method=O_DIRECT if possible (this is also OS specific))
6. defragment tables, rebuild indexes, do table maintenance
7. If you use innodb_flush_txn_commit=1, use a battery-backed hardware cache write controller
8. more RAM is good so faster disk speed
9. use 64-bit architectures
10. --skip-name-resolve
11. increase myisam_sort_buffer_size to optimize large inserts (this is a per-connection variable)
12. look up memory tuning parameter for on-insert caching
13. increase temp table size in a data warehousing environment (default is 32Mb) so it doesn't write to disk (also constrained by max_heap_table_size, default 16Mb)
14. Run in SQL_MODE=STRICT to help identify warnings
15. /tmp dir on battery-backed write cache
16. consider battery-backed RAM for innodb logfiles
17. use --safe-updates for client
18. Redundant data is redundant
Storage Engine Performance Tips:
1. InnoDB ALWAYS keeps the primary key as part of each index, so do not make the primary key very large
2. Utilize different storage engines on master/slave ie, if you need fulltext indexing on a table.
3. BLACKHOLE engine and replication is much faster than FEDERATED tables for things like logs.
4. Know your storage engines and what performs best for your needs, know that different ones exist.
1. ie, use MERGE tables ARCHIVE tables for logs
2. Archive old data -- don't be a pack-rat! 2 common engines for this are ARCHIVE tables and MERGE tables
5. use row-level instead of table-level locking for OLTP workloads
6. try out a few schemas and storage engines in your test environment before picking one.
Database Design Performance Tips:
1. Design sane query schemas. don't be afraid of table joins, often they are faster than denormalization
2. Don't use boolean flags
3. Use Indexes
4. Don't Index Everything
5. Do not duplicate indexes
6. Do not use large columns in indexes if the ratio of SELECTs:INSERTs is low.
7. be careful of redundant columns in an index or across indexes
8. Use a clever key and ORDER BY instead of MAX
9. Normalize first, and denormalize where appropriate.
10. Databases are not spreadsheets, even though Access really really looks like one. Then again, Access isn't a real database
11. use INET_ATON and INET_NTOA for IP addresses, not char or varchar
12. make it a habit to REVERSE() email addresses, so you can easily search domains (this will help avoid wildcards at the start of LIKE queries if you want to find everyone whose e-mail is in a certain domain)
13. A NULL data type can take more room to store than NOT NULL
14. Choose appropriate character sets & collations -- UTF16 will store each character in 2 bytes, whether it needs it or not, latin1 is faster than UTF8.
15. Use Triggers wisely
16. use min_rows and max_rows to specify approximate data size so space can be pre-allocated and reference points can be calculated.
17. Use HASH indexing for indexing across columns with similar data prefixes
18. Use myisam_pack_keys for int data
19. be able to change your schema without ruining functionality of your code
20. segregate tables/databases that benefit from different configuration variables
Other:
1. Hire a MySQL (tm) Certified DBA
2. Know that there are many consulting companies out there that can help, as well as MySQL's Professional Services.
3. Read and post to MySQL Planet at http://www.planetmysql.org
4. Attend the yearly MySQL Conference and Expo or other conferences with MySQL tracks (link to the conference here)
5. Support your local User Group (link to forge page w/user groups here)
Authored by
Jay Pipes, Sheeri Kritzer, Bill Karwin, Ronald Bradford, Farhan "Frank Mash" Mashraqi, Taso Du Val, Ron Hu, Klinton Lee, Rick James, Alan Kasindorf, Eric Bergen, Kaj Arno, Joel Seligstein, Amy Lee
Specific Query Performance Tips (see also database design tips for tips on indexes):
1. Use EXPLAIN to profile the query execution plan
2. Use Slow Query Log (always have it on!)
3. Don't use DISTINCT when you have or could use GROUP BY
4. Insert performance
1. Batch INSERT and REPLACE
2. Use LOAD DATA instead of INSERT
5. LIMIT m,n may not be as fast as it sounds
6. Don't use ORDER BY RAND() if you have > ~2K records
7. Use SQL_NO_CACHE when you are SELECTing frequently updated data or large sets of data
8. Avoid wildcards at the start of LIKE queries
9. Avoid correlated subqueries and in select and where clause (try to avoid in)
10. No calculated comparisons -- isolate indexed columns
11. ORDER BY and LIMIT work best with equalities and covered indexes
12. Separate text/blobs from metadata, don't put text/blobs in results if you don't need them
13. Derived tables (subqueries in the FROM clause) can be useful for retrieving BLOBs without sorting them. (Self-join can speed up a query if 1st part finds the IDs and uses then to fetch the rest)
14. ALTER TABLE...ORDER BY can take data sorted chronologically and re-order it by a different field -- this can make queries on that field run faster (maybe this goes in indexing?)
15. Know when to split a complex query and join smaller ones
16. Delete small amounts at a time if you can
17. Make similar queries consistent so cache is used
18. Have good SQL query standards
19. Don't use deprecated features
20. Turning OR on multiple index fields (<5.0) into UNION may speed things up (with LIMIT), after 5.0 the index_merge should pick stuff up.
21. Don't use COUNT * on Innodb tables for every search, do it a few times and/or summary tables, or if you need it for the total # of rows, use SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS()
22. Use INSERT ... ON DUPLICATE KEY update (INSERT IGNORE) to avoid having to SELECT
23. use groupwise maximum instead of subqueries
Scaling Performance Tips:
1. Use benchmarking
2. isolate workloads don't let administrative work interfere with customer performance. (ie backups)
3. Debugging sucks, testing rocks!
4. As your data grows, indexing may change (cardinality and selectivity change). Structuring may want to change. Make your schema as modular as your code. Make your code able to scale. Plan and embrace change, and get developers to do the same.
Network Performance Tips:
1. Minimize traffic by fetching only what you need.
1. Paging/chunked data retrieval to limit
2. Don't use SELECT *
3. Be wary of lots of small quick queries if a longer query can be more efficient
2. Use multi_query if appropriate to reduce round-trips
3. Use stored procedures to avoid bandwidth wastage
OS Performance Tips:
1. Use proper data partitions
1. For Cluster. Start thinking about Cluster *before* you need them
2. Keep the database host as clean as possible. Do you really need a windowing system on that server?
3. Utilize the strengths of the OS
4. pare down cron scripts
5. create a test environment
6. source control schema and config files
7. for LVM innodb backups, restore to a different instance of MySQL so Innodb can roll forward
8. partition appropriately
9. partition your database when you have real data -- do not assume you know your dataset until you have real data
MySQL Server Overall Tips:
1. innodb_flush_commit=0 can help slave lag
2. Optimize for data types, use consistent data types. Use PROCEDURE ANALYSE() to help determine the smallest data type for your needs.
3. use optimistic locking, not pessimistic locking. try to use shared lock, not exclusive lock. share mode vs. FOR UPDATE
4. if you can, compress text/blobs
5. compress static data
6. don't back up static data as often
7. enable and increase the query and buffer caches if appropriate
8. config params -- http://docs.cellblue.nl/2007/03/17/easy-mysql-performance-tweaks/ is a good reference
9. Config variables & tips:
1. use one of the supplied config files
2. key_buffer, unix cache (leave some RAM free), per-connection variables, innodb memory variables
3. be aware of global vs. per-connection variables
4. check SHOW STATUS and SHOW VARIABLES (GLOBAL|SESSION in 5.0 and up)
5. be aware of swapping esp. with Linux, "swappiness" (bypass OS filecache for innodb data files, innodb_flush_method=O_DIRECT if possible (this is also OS specific))
6. defragment tables, rebuild indexes, do table maintenance
7. If you use innodb_flush_txn_commit=1, use a battery-backed hardware cache write controller
8. more RAM is good so faster disk speed
9. use 64-bit architectures
10. --skip-name-resolve
11. increase myisam_sort_buffer_size to optimize large inserts (this is a per-connection variable)
12. look up memory tuning parameter for on-insert caching
13. increase temp table size in a data warehousing environment (default is 32Mb) so it doesn't write to disk (also constrained by max_heap_table_size, default 16Mb)
14. Run in SQL_MODE=STRICT to help identify warnings
15. /tmp dir on battery-backed write cache
16. consider battery-backed RAM for innodb logfiles
17. use --safe-updates for client
18. Redundant data is redundant
Storage Engine Performance Tips:
1. InnoDB ALWAYS keeps the primary key as part of each index, so do not make the primary key very large
2. Utilize different storage engines on master/slave ie, if you need fulltext indexing on a table.
3. BLACKHOLE engine and replication is much faster than FEDERATED tables for things like logs.
4. Know your storage engines and what performs best for your needs, know that different ones exist.
1. ie, use MERGE tables ARCHIVE tables for logs
2. Archive old data -- don't be a pack-rat! 2 common engines for this are ARCHIVE tables and MERGE tables
5. use row-level instead of table-level locking for OLTP workloads
6. try out a few schemas and storage engines in your test environment before picking one.
Database Design Performance Tips:
1. Design sane query schemas. don't be afraid of table joins, often they are faster than denormalization
2. Don't use boolean flags
3. Use Indexes
4. Don't Index Everything
5. Do not duplicate indexes
6. Do not use large columns in indexes if the ratio of SELECTs:INSERTs is low.
7. be careful of redundant columns in an index or across indexes
8. Use a clever key and ORDER BY instead of MAX
9. Normalize first, and denormalize where appropriate.
10. Databases are not spreadsheets, even though Access really really looks like one. Then again, Access isn't a real database
11. use INET_ATON and INET_NTOA for IP addresses, not char or varchar
12. make it a habit to REVERSE() email addresses, so you can easily search domains (this will help avoid wildcards at the start of LIKE queries if you want to find everyone whose e-mail is in a certain domain)
13. A NULL data type can take more room to store than NOT NULL
14. Choose appropriate character sets & collations -- UTF16 will store each character in 2 bytes, whether it needs it or not, latin1 is faster than UTF8.
15. Use Triggers wisely
16. use min_rows and max_rows to specify approximate data size so space can be pre-allocated and reference points can be calculated.
17. Use HASH indexing for indexing across columns with similar data prefixes
18. Use myisam_pack_keys for int data
19. be able to change your schema without ruining functionality of your code
20. segregate tables/databases that benefit from different configuration variables
Other:
1. Hire a MySQL (tm) Certified DBA
2. Know that there are many consulting companies out there that can help, as well as MySQL's Professional Services.
3. Read and post to MySQL Planet at http://www.planetmysql.org
4. Attend the yearly MySQL Conference and Expo or other conferences with MySQL tracks (link to the conference here)
5. Support your local User Group (link to forge page w/user groups here)
Authored by
Jay Pipes, Sheeri Kritzer, Bill Karwin, Ronald Bradford, Farhan "Frank Mash" Mashraqi, Taso Du Val, Ron Hu, Klinton Lee, Rick James, Alan Kasindorf, Eric Bergen, Kaj Arno, Joel Seligstein, Amy Lee
2009年3月8日星期日
分析Windows和Linux动态库
原文见:http://tech.21ds.net/category/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/linux/
摘要:动态链接库技术实现和设计程序常用的技术,在Windows和Linux系统中都有动态库的概念,采用动态库可以有效的减少程序大小,节省空间,提高效率,增加程序的可扩展性,便于模块化管理。但不同操作系统的动态库由于格式不同,在需要不同操作系统调用时需要进行动态库程序移植。本文分析和比较了两种操作系统动态库技术,并给出了将Visual C++编制的动态库移植到Linux上的方法和经验。
1、引言
动态库(Dynamic Link Library abbr,DLL)技术是程序设计中经常采用的技术。其目的减少程序的大小,节省空间,提高效率,具有很高的灵活性。采用动态库技术对于升级软件版本更加容易。与静态库(Static Link Library)不同,动态库里面的函数不是执行程序本身的一部分,而是根据执行需要按需载入,其执行代码可以同时在多个程序中共享。
在Windows和Linux操作系统中,都可采用这种方式进行软件设计,但他们的调用方式以及程序编制方式不尽相同。本文首先分析了在这两种操作系统中通常采用的动态库调用方法以及程序编制方式,然后分析比较了这两种方式的不同之处,最后根据实际移植程序经验,介绍了将VC++编制的 Windows动态库移植到Linux下的方法。
2、动态库技术
2.1 Windows动态库技术
动态链接库是实现Windows应用程序共享资源、节省内存空间、提高使用效率的一个重要技术手段。常见的动态库包含外部函数和资源,也有一些动态库只包含资源,如Windows字体资源文件,称之为资源动态链接库。通常动态库以.dll,.drv、.fon等作为后缀。相应的windows静态库通常以.lib结尾,Windows自己就将一些主要的系统功能以动态库模块的形式实现。
Windows动态库在运行时被系统加载到进程的虚拟空间中,使用从调用进程的虚拟地址空间分配的内存,成为调用进程的一部分。DLL也只能被该进程的线程所访问。DLL的句柄可以被调用进程使用;调用进程的句柄可以被DLL使用。DLL模块中包含各种导出函数,用于向外界提供服务。DLL可以有自己的数据段,但没有自己的堆栈,使用与调用它的应用程序相同的堆栈模式;一个DLL在内存中只有一个实例;DLL实现了代码封装性;DLL的编制与具体的编程语言及编译器无关,可以通过DLL来实现混合语言编程。DLL函数中的代码所创建的任何对象(包括变量)都归调用它的线程或进程所有。
根据调用方式的不同,对动态库的调用可分为静态调用方式和动态调用方式。
(1)静态调用,也称为隐式调用,由编译系统完成对DLL的加载和应用程序结束时DLL卸载的编码(Windows系统负责对DLL调用次数的计数),调用方式简单,能够满足通常的要求。通常采用的调用方式是把产生动态连接库时产生的.LIB文件加入到应用程序的工程中,想使用DLL中的函数时,只须在源文件中声明一下。 LIB文件包含了每一个DLL导出函数的符号名和可选择的标识号以及DLL文件名,不含有实际的代码。Lib文件包含的信息进入到生成的应用程序中,被调用的DLL文件会在应用程序加载时同时加载在到内存中。
(2)动态调用,即显式调用方式,是由编程者用API函数加载和卸载DLL来达到调用DLL的目的,比较复杂,但能更加有效地使用内存,是编制大型应用程序时的重要方式。在Windows系统中,与动态库调用有关的函数包括:
①LoadLibrary(或MFC 的AfxLoadLibrary),装载动态库。
②GetProcAddress,获取要引入的函数,将符号名或标识号转换为DLL内部地址。
③FreeLibrary(或MFC的AfxFreeLibrary),释放动态链接库。
在windows中创建动态库也非常方便和简单。在Visual C++中,可以创建不用MFC而直接用C语言写的DLL程序,也可以创建基于MFC类库的DLL程序。每一个DLL必须有一个入口点,在VC++中, DllMain是一个缺省的入口函数。DllMain负责初始化(Initialization)和结束(Termination)工作。动态库输出函数也有两种约定,分别是基于调用约定和名字修饰约定。DLL程序定义的函数分为内部函数和导出函数,动态库导出的函数供其它程序模块调用。通常可以有下面几种方法导出函数:
①采用模块定义文件的EXPORT部分指定要输入的函数或者变量。
②使用MFC提供的修饰符号_declspec(dllexport)。
③以命令行方式,采用/EXPORT命令行输出有关函数。
在windows动态库中,有时需要编写模块定义文件(.DEF),它是用于描述DLL属性的模块语句组成的文本文件。
2.2 Linux共享对象技术
在Linux操作系统中,采用了很多共享对象技术(Shared Object),虽然它和Windows里的动态库相对应,但它并不称为动态库。相应的共享对象文件以.so作为后缀,为了方便,在本文中,对该概念不进行专门区分。Linux系统的/lib以及标准图形界面的/usr/X11R6/lib等目录里面,就有许多以so结尾的共享对象。同样,在Linux 下,也有静态函数库这种调用方式,相应的后缀以.a结束。Linux采用该共享对象技术以方便程序间共享,节省程序占有空间,增加程序的可扩展性和灵活性。Linux还可以通过LD-PRELOAD变量让开发人员可以使用自己的程序库中的模块来替换系统模块。
同Windows系统一样,在Linux中创建和使用动态库是比较容易的事情,在编译函数库源程序时加上-shared选项即可,这样所生成的执行程序就是动态链接库。通常这样的程序以so为后缀,在Linux动态库程序设计过程中,通常流程是编写用户的接口文件,通常是.h文件,编写实际的函数文件,以.c或.cpp为后缀,再编写makefile文件。对于较小的动态库程序可以不用如此,但这样设计使程序更加合理。
编译生成动态连接库后,进而可以在程序中进行调用。在Linux中,可以采用多种调用方式,同Windows的系统目录(..\ system32等)一样,可以将动态库文件拷贝到/lib目录或者在/lib目录里面建立符号连接,以便所有用户使用。下面介绍Linux调用动态库经常使用的函数,但在使用动态库时,源程序必须包含dlfcn.h头文件,该文件定义调用动态链接库的函数的原型。
(1)_打开动态链接库:dlopen,函数原型void *dlopen (const char *filename, int flag);
dlopen用于打开指定名字(filename)的动态链接库,并返回操作句柄。
(2)取函数执行地址:dlsym,函数原型为: void *dlsym(void *handle, char *symbol);
dlsym根据动态链接库操作句柄(handle)与符号(symbol),返回符号对应的函数的执行代码地址。
(3)关闭动态链接库:dlclose,函数原型为: int dlclose (void *handle);
dlclose用于关闭指定句柄的动态链接库,只有当此动态链接库的使用计数为0时,才会真正被系统卸载。
(4)动态库错误函数:dlerror,函数原型为: const char *dlerror(void); 当动态链接库操作函数执行失败时,dlerror可以返回出错信息,返回值为NULL时表示操作函数执行成功。
在取到函数执行地址后,就可以在动态库的使用程序里面根据动态库提供的函数接口声明调用动态库里面的函数。在编写调用动态库的程序的makefile文件时,需要加入编译选项-rdynamic和-ldl。
除了采用这种方式编写和调用动态库之外,Linux操作系统也提供了一种更为方便的动态库调用方式,也方便了其它程序调用,这种方式与 Windows系统的隐式链接类似。其动态库命名方式为“lib*.so.*”。在这个命名方式中,第一个*表示动态链接库的库名,第二个*通常表示该动态库的版本号,也可以没有版本号。在这种调用方式中,需要维护动态链接库的配置文件/etc/ld.so.conf来让动态链接库为系统所使用,通常将动态链接库所在目录名追加到动态链接库配置文件中。如具有X window窗口系统发行版该文件中都具有/usr/X11R6/lib,它指向X window窗口系统的动态链接库所在目录。为了使动态链接库能为系统所共享,还需运行动态链接库的管理命令./sbin/ldconfig。在编译所引用的动态库时,可以在gcc采用 ?l或-L选项或直接引用所需的动态链接库方式进行编译。在Linux里面,可以采用ldd命令来检查程序依赖共享库。
3、两种系统动态库比较分析
Windows和Linux采用动态链接库技术目的是基本一致的,但由于操作系统的不同,他们在许多方面还是不尽相同,下面从以下几个方面进行阐述。
(1)动态库程序编写,在Windows系统下的执行文件格式是PE格式,动态库需要一个DllMain函数作为初始化的人口,通常在导出函数的声明时需要有_declspec(dllexport)关键字。Linux下的gcc编译的执行文件默认是ELF格式,不需要初始化入口,亦不需要到函数做特别声明,编写比较方便。
(2)动态库编译,在windows系统下面,有方便的调试编译环境,通常不用自己去编写makefile文件,但在linux下面,需要自己动手去编写makefile文件,因此,必须掌握一定的makefile编写技巧,另外,通常Linux编译规则相对严格。
(3)动态库调用方面,Windows和Linux对其下编制的动态库都可以采用显式调用或隐式调用,但具体的调用方式也不尽相同。
(4)动态库输出函数查看,在Windows中,有许多工具和软件可以进行查看DLL中所输出的函数,例如命令行方式的dumpbin以及VC ++工具中的DEPENDS程序。在Linux系统中通常采用nm来查看输出函数,也可以使用ldd查看程序隐式链接的共享对象文件。
(5)对操作系统的依赖,这两种动态库运行依赖于各自的操作系统,不能跨平台使用。因此,对于实现相同功能的动态库,必须为两种不同的操作系统提供不同的动态库版本。
4、动态库移植方法
如果要编制在两个系统中都能使用的动态链接库,通常会先选择在Windows的VC++提供的调试环境中完成初始的开发,毕竟VC++ 提供的图形化编辑和调试界面比vi和gcc方便许多。完成测试之后,再进行动态库的程序移植。通常gcc默认的编译规则比VC++默认的编译规则严格,即使在VC++下面没有任何警告错误的程序在gcc调试中也会出现许多警告错误,可以在gcc中采用-w选项关闭警告错误。
下面给出程序移植需要遵循的规则以及经验。
(1)尽量不要改变原有动态库头文件的顺序。通常在C/C++语言中,头文件的顺序有相当的关系。另外虽然C/C++语言区分大小写,但在包含头文件时,Linux必须与头文件的大小写相同,因为ext2文件系统对文件名是大小写敏感,否则不能正确编译,而在Windows下面,头文件大小写可以正确编译。
(2)不同系统独有的头文件。在Windows系统中,通常会包括windows.h头文件,如果调用底层的通信函数,则会包含 winsock..h头文件。因此在移植到Linux系统时,要注释掉这些Windows系统独有的头文件以及一些windows系统的常量定义说明,增加Linux都底层通信的支持的头文件等。
(3)数据类型。VC++具有许多独有的数据类型,如__int16,__int32, TRUE,SOCKET等,gcc编译器不支持它们。通常做法是需要将windows.h和basetypes.h中对这些数据进行定义的语句复制到一个头文件中,再在Linux中包含这个头文件。例如将套接字的类型为SOCKET改为int。
(4)关键字。VC++中具有许多标准C中所没有采用的关键字,如BOOL,BYTE,DWORD,__asm等,通常在为了移植方便,尽量不使用它们,如果实在无法避免可以采用#ifdef 和#endif为LINUX和WINDOWS编写两个版本。
(5)函数原型的修改。通常如果采用标准的C/C++语言编写的动态库,基本上不用再重新编写函数,但对于系统调用函数,由于两种系统的区别,需要改变函数的调用方式等,如在Linux编制的网络通信动态库中,用close()函数代替windows操作系统下的closesocket()函数来关闭套接字。另外在Linux下没有文件句柄,要打开文件可用open和fopen函数,具体这两个函数的用法可参考文献[2]。
(6) makefile的编写。在windows下面通常由VC++编译器来负责调试,但gcc需要自己动手编写makefile文件,也可以参照VC++生成的makefile文件。对于动态库移植,编译动态库时需要加入-shared选项。对于采用数学函数,如幂级数的程序,在调用动态库是,需要加入- lm。
(7)其它一些需要注意的地方
①程序设计结构分析,对于移植它人编写的动态库程序,程序结构分析是必不可少的步骤,通常在动态库程序中,不会包含界面等操作,所以相对容易一些。
②在Linux中,对文件或目录的权限分为拥有者、群组、其它。所以在存取文件时,要注意对文件是读还是写操作,如果是对文件进行写操作,要注意修改文件或目录的权限,否则无法对文件进行写。
③指针的使用,定义一个指针只给它分配四个字节的内存,如果要对指针所指向的变量赋值,必须用malloc函数为它分配内存或不把它定义为指针而定义为变量即可,这点在linux下面比windows编译严格。同样结构不能在函数中传值,如果要在函数中进行结构传值,必须把函数中的结构定义为结构指针。
④路径标识符,在Linux下是“/”,在Windows下是“\”,注意windows和Linux的对动态库搜索路径的不同。
⑤编程和调试技巧方面。对不同的调试环境有不同的调试技巧,在这里不多叙述。
5、结束语
本文系统分析了windows和Linux动态库实现和使用方式,从程序编写、编译、调用以及对操作系统依赖等方面综合分析比较了这两种调用方式的不同之处,根据实际程序移植经验,给出了将VC++编制的Windows动态库移植到Linux下的方法以及需要注意的问题,同时并给出了程序示例片断,实际在程序移植过程中,由于系统的设计等方面,可能移植起来需要注意的方面远比上面复杂,本文通过总结归纳进而为不同操作系统程序移植提供了有意的经验和技巧。
摘要:动态链接库技术实现和设计程序常用的技术,在Windows和Linux系统中都有动态库的概念,采用动态库可以有效的减少程序大小,节省空间,提高效率,增加程序的可扩展性,便于模块化管理。但不同操作系统的动态库由于格式不同,在需要不同操作系统调用时需要进行动态库程序移植。本文分析和比较了两种操作系统动态库技术,并给出了将Visual C++编制的动态库移植到Linux上的方法和经验。
1、引言
动态库(Dynamic Link Library abbr,DLL)技术是程序设计中经常采用的技术。其目的减少程序的大小,节省空间,提高效率,具有很高的灵活性。采用动态库技术对于升级软件版本更加容易。与静态库(Static Link Library)不同,动态库里面的函数不是执行程序本身的一部分,而是根据执行需要按需载入,其执行代码可以同时在多个程序中共享。
在Windows和Linux操作系统中,都可采用这种方式进行软件设计,但他们的调用方式以及程序编制方式不尽相同。本文首先分析了在这两种操作系统中通常采用的动态库调用方法以及程序编制方式,然后分析比较了这两种方式的不同之处,最后根据实际移植程序经验,介绍了将VC++编制的 Windows动态库移植到Linux下的方法。
2、动态库技术
2.1 Windows动态库技术
动态链接库是实现Windows应用程序共享资源、节省内存空间、提高使用效率的一个重要技术手段。常见的动态库包含外部函数和资源,也有一些动态库只包含资源,如Windows字体资源文件,称之为资源动态链接库。通常动态库以.dll,.drv、.fon等作为后缀。相应的windows静态库通常以.lib结尾,Windows自己就将一些主要的系统功能以动态库模块的形式实现。
Windows动态库在运行时被系统加载到进程的虚拟空间中,使用从调用进程的虚拟地址空间分配的内存,成为调用进程的一部分。DLL也只能被该进程的线程所访问。DLL的句柄可以被调用进程使用;调用进程的句柄可以被DLL使用。DLL模块中包含各种导出函数,用于向外界提供服务。DLL可以有自己的数据段,但没有自己的堆栈,使用与调用它的应用程序相同的堆栈模式;一个DLL在内存中只有一个实例;DLL实现了代码封装性;DLL的编制与具体的编程语言及编译器无关,可以通过DLL来实现混合语言编程。DLL函数中的代码所创建的任何对象(包括变量)都归调用它的线程或进程所有。
根据调用方式的不同,对动态库的调用可分为静态调用方式和动态调用方式。
(1)静态调用,也称为隐式调用,由编译系统完成对DLL的加载和应用程序结束时DLL卸载的编码(Windows系统负责对DLL调用次数的计数),调用方式简单,能够满足通常的要求。通常采用的调用方式是把产生动态连接库时产生的.LIB文件加入到应用程序的工程中,想使用DLL中的函数时,只须在源文件中声明一下。 LIB文件包含了每一个DLL导出函数的符号名和可选择的标识号以及DLL文件名,不含有实际的代码。Lib文件包含的信息进入到生成的应用程序中,被调用的DLL文件会在应用程序加载时同时加载在到内存中。
(2)动态调用,即显式调用方式,是由编程者用API函数加载和卸载DLL来达到调用DLL的目的,比较复杂,但能更加有效地使用内存,是编制大型应用程序时的重要方式。在Windows系统中,与动态库调用有关的函数包括:
①LoadLibrary(或MFC 的AfxLoadLibrary),装载动态库。
②GetProcAddress,获取要引入的函数,将符号名或标识号转换为DLL内部地址。
③FreeLibrary(或MFC的AfxFreeLibrary),释放动态链接库。
在windows中创建动态库也非常方便和简单。在Visual C++中,可以创建不用MFC而直接用C语言写的DLL程序,也可以创建基于MFC类库的DLL程序。每一个DLL必须有一个入口点,在VC++中, DllMain是一个缺省的入口函数。DllMain负责初始化(Initialization)和结束(Termination)工作。动态库输出函数也有两种约定,分别是基于调用约定和名字修饰约定。DLL程序定义的函数分为内部函数和导出函数,动态库导出的函数供其它程序模块调用。通常可以有下面几种方法导出函数:
①采用模块定义文件的EXPORT部分指定要输入的函数或者变量。
②使用MFC提供的修饰符号_declspec(dllexport)。
③以命令行方式,采用/EXPORT命令行输出有关函数。
在windows动态库中,有时需要编写模块定义文件(.DEF),它是用于描述DLL属性的模块语句组成的文本文件。
2.2 Linux共享对象技术
在Linux操作系统中,采用了很多共享对象技术(Shared Object),虽然它和Windows里的动态库相对应,但它并不称为动态库。相应的共享对象文件以.so作为后缀,为了方便,在本文中,对该概念不进行专门区分。Linux系统的/lib以及标准图形界面的/usr/X11R6/lib等目录里面,就有许多以so结尾的共享对象。同样,在Linux 下,也有静态函数库这种调用方式,相应的后缀以.a结束。Linux采用该共享对象技术以方便程序间共享,节省程序占有空间,增加程序的可扩展性和灵活性。Linux还可以通过LD-PRELOAD变量让开发人员可以使用自己的程序库中的模块来替换系统模块。
同Windows系统一样,在Linux中创建和使用动态库是比较容易的事情,在编译函数库源程序时加上-shared选项即可,这样所生成的执行程序就是动态链接库。通常这样的程序以so为后缀,在Linux动态库程序设计过程中,通常流程是编写用户的接口文件,通常是.h文件,编写实际的函数文件,以.c或.cpp为后缀,再编写makefile文件。对于较小的动态库程序可以不用如此,但这样设计使程序更加合理。
编译生成动态连接库后,进而可以在程序中进行调用。在Linux中,可以采用多种调用方式,同Windows的系统目录(..\ system32等)一样,可以将动态库文件拷贝到/lib目录或者在/lib目录里面建立符号连接,以便所有用户使用。下面介绍Linux调用动态库经常使用的函数,但在使用动态库时,源程序必须包含dlfcn.h头文件,该文件定义调用动态链接库的函数的原型。
(1)_打开动态链接库:dlopen,函数原型void *dlopen (const char *filename, int flag);
dlopen用于打开指定名字(filename)的动态链接库,并返回操作句柄。
(2)取函数执行地址:dlsym,函数原型为: void *dlsym(void *handle, char *symbol);
dlsym根据动态链接库操作句柄(handle)与符号(symbol),返回符号对应的函数的执行代码地址。
(3)关闭动态链接库:dlclose,函数原型为: int dlclose (void *handle);
dlclose用于关闭指定句柄的动态链接库,只有当此动态链接库的使用计数为0时,才会真正被系统卸载。
(4)动态库错误函数:dlerror,函数原型为: const char *dlerror(void); 当动态链接库操作函数执行失败时,dlerror可以返回出错信息,返回值为NULL时表示操作函数执行成功。
在取到函数执行地址后,就可以在动态库的使用程序里面根据动态库提供的函数接口声明调用动态库里面的函数。在编写调用动态库的程序的makefile文件时,需要加入编译选项-rdynamic和-ldl。
除了采用这种方式编写和调用动态库之外,Linux操作系统也提供了一种更为方便的动态库调用方式,也方便了其它程序调用,这种方式与 Windows系统的隐式链接类似。其动态库命名方式为“lib*.so.*”。在这个命名方式中,第一个*表示动态链接库的库名,第二个*通常表示该动态库的版本号,也可以没有版本号。在这种调用方式中,需要维护动态链接库的配置文件/etc/ld.so.conf来让动态链接库为系统所使用,通常将动态链接库所在目录名追加到动态链接库配置文件中。如具有X window窗口系统发行版该文件中都具有/usr/X11R6/lib,它指向X window窗口系统的动态链接库所在目录。为了使动态链接库能为系统所共享,还需运行动态链接库的管理命令./sbin/ldconfig。在编译所引用的动态库时,可以在gcc采用 ?l或-L选项或直接引用所需的动态链接库方式进行编译。在Linux里面,可以采用ldd命令来检查程序依赖共享库。
3、两种系统动态库比较分析
Windows和Linux采用动态链接库技术目的是基本一致的,但由于操作系统的不同,他们在许多方面还是不尽相同,下面从以下几个方面进行阐述。
(1)动态库程序编写,在Windows系统下的执行文件格式是PE格式,动态库需要一个DllMain函数作为初始化的人口,通常在导出函数的声明时需要有_declspec(dllexport)关键字。Linux下的gcc编译的执行文件默认是ELF格式,不需要初始化入口,亦不需要到函数做特别声明,编写比较方便。
(2)动态库编译,在windows系统下面,有方便的调试编译环境,通常不用自己去编写makefile文件,但在linux下面,需要自己动手去编写makefile文件,因此,必须掌握一定的makefile编写技巧,另外,通常Linux编译规则相对严格。
(3)动态库调用方面,Windows和Linux对其下编制的动态库都可以采用显式调用或隐式调用,但具体的调用方式也不尽相同。
(4)动态库输出函数查看,在Windows中,有许多工具和软件可以进行查看DLL中所输出的函数,例如命令行方式的dumpbin以及VC ++工具中的DEPENDS程序。在Linux系统中通常采用nm来查看输出函数,也可以使用ldd查看程序隐式链接的共享对象文件。
(5)对操作系统的依赖,这两种动态库运行依赖于各自的操作系统,不能跨平台使用。因此,对于实现相同功能的动态库,必须为两种不同的操作系统提供不同的动态库版本。
4、动态库移植方法
如果要编制在两个系统中都能使用的动态链接库,通常会先选择在Windows的VC++提供的调试环境中完成初始的开发,毕竟VC++ 提供的图形化编辑和调试界面比vi和gcc方便许多。完成测试之后,再进行动态库的程序移植。通常gcc默认的编译规则比VC++默认的编译规则严格,即使在VC++下面没有任何警告错误的程序在gcc调试中也会出现许多警告错误,可以在gcc中采用-w选项关闭警告错误。
下面给出程序移植需要遵循的规则以及经验。
(1)尽量不要改变原有动态库头文件的顺序。通常在C/C++语言中,头文件的顺序有相当的关系。另外虽然C/C++语言区分大小写,但在包含头文件时,Linux必须与头文件的大小写相同,因为ext2文件系统对文件名是大小写敏感,否则不能正确编译,而在Windows下面,头文件大小写可以正确编译。
(2)不同系统独有的头文件。在Windows系统中,通常会包括windows.h头文件,如果调用底层的通信函数,则会包含 winsock..h头文件。因此在移植到Linux系统时,要注释掉这些Windows系统独有的头文件以及一些windows系统的常量定义说明,增加Linux都底层通信的支持的头文件等。
(3)数据类型。VC++具有许多独有的数据类型,如__int16,__int32, TRUE,SOCKET等,gcc编译器不支持它们。通常做法是需要将windows.h和basetypes.h中对这些数据进行定义的语句复制到一个头文件中,再在Linux中包含这个头文件。例如将套接字的类型为SOCKET改为int。
(4)关键字。VC++中具有许多标准C中所没有采用的关键字,如BOOL,BYTE,DWORD,__asm等,通常在为了移植方便,尽量不使用它们,如果实在无法避免可以采用#ifdef 和#endif为LINUX和WINDOWS编写两个版本。
(5)函数原型的修改。通常如果采用标准的C/C++语言编写的动态库,基本上不用再重新编写函数,但对于系统调用函数,由于两种系统的区别,需要改变函数的调用方式等,如在Linux编制的网络通信动态库中,用close()函数代替windows操作系统下的closesocket()函数来关闭套接字。另外在Linux下没有文件句柄,要打开文件可用open和fopen函数,具体这两个函数的用法可参考文献[2]。
(6) makefile的编写。在windows下面通常由VC++编译器来负责调试,但gcc需要自己动手编写makefile文件,也可以参照VC++生成的makefile文件。对于动态库移植,编译动态库时需要加入-shared选项。对于采用数学函数,如幂级数的程序,在调用动态库是,需要加入- lm。
(7)其它一些需要注意的地方
①程序设计结构分析,对于移植它人编写的动态库程序,程序结构分析是必不可少的步骤,通常在动态库程序中,不会包含界面等操作,所以相对容易一些。
②在Linux中,对文件或目录的权限分为拥有者、群组、其它。所以在存取文件时,要注意对文件是读还是写操作,如果是对文件进行写操作,要注意修改文件或目录的权限,否则无法对文件进行写。
③指针的使用,定义一个指针只给它分配四个字节的内存,如果要对指针所指向的变量赋值,必须用malloc函数为它分配内存或不把它定义为指针而定义为变量即可,这点在linux下面比windows编译严格。同样结构不能在函数中传值,如果要在函数中进行结构传值,必须把函数中的结构定义为结构指针。
④路径标识符,在Linux下是“/”,在Windows下是“\”,注意windows和Linux的对动态库搜索路径的不同。
⑤编程和调试技巧方面。对不同的调试环境有不同的调试技巧,在这里不多叙述。
5、结束语
本文系统分析了windows和Linux动态库实现和使用方式,从程序编写、编译、调用以及对操作系统依赖等方面综合分析比较了这两种调用方式的不同之处,根据实际程序移植经验,给出了将VC++编制的Windows动态库移植到Linux下的方法以及需要注意的问题,同时并给出了程序示例片断,实际在程序移植过程中,由于系统的设计等方面,可能移植起来需要注意的方面远比上面复杂,本文通过总结归纳进而为不同操作系统程序移植提供了有意的经验和技巧。
2009年3月5日星期四
Usefull Commands: Video
Published
by
kev
2 years, 3 months ago
in English
* Change the aspect ratio of a film for the playback. Standard aspect ratio are : 1.33 (4:3), 1.66 (1.66:1), 1.77 (16:9) and 2.35 (2.35:1):
mplayer -aspect 2:1 ./video.avi
* Play the video with subtitles:
mplayer -sub ./subtitle_file.txt ./video.avi
* This will extract audio track no. 128, downmix the AC3 sound to PCM and write the results to file.wav:
mplayer -vo null -hardframedrop -aid 128 -ao pcm -aofile file.wav dvd://1
* This will extract the audio, convert it to PCM and write the resulting wave file to audio.wav:
mplayer -vo null -hardframedrop -ao pcm:file=audio.wav myvideo.avi
* Extract to chapter.txt the chapter file of the track n°1 of the DVD:
dvdxchap -t 1 /mnt/cdrom > chapter.txt
* Show all subtitles streams:
mplayer -vo null -ao null -frames 0 -v 2 dvd://1 >&1 | grep sid
* Extract the raw subtitle stream. The -a 0x21 option correspond to the subtitle stream’s hexadecimal number (= 0×20 + id of the stream):
tccat -i /space/st-tng/dic1/ -T 1 -L | tcextract -x ps1 -t vob -a 0x22 > subs-en
* Create a rotated copy of the file.avi video (rotate=1 : clockwise ; rotate=2 : anti-clockwise):
mencoder -vop rotate=2 -oac pcm -ovc lavc ./source.avi -o ./dest.avi
* Preview a video composed of all jpeg files from the current folder at 15fps (mplayer only support jpeg, png, tga and sgi formats):
mplayer "mf://*.jpg" -mf fps=15
* Create a 15fps video from all jpeg files of the current folder:
mencoder "mf://*.jpg" -mf fps=15 -ovc lavc -o ./dest.avi
* Encode a SVCD to AVI file:
mencoder -oac lavc -ovc lavc vcd://1 -o ./svcd.avi
* Transcode video to raw format (be carefull: usually the output video got annoying audio delay):
mencoder -oac pcm -ovc raw -ofps 25 -noskip ./video.wmv -o ./video.avi
* Encode a video using the default mpeg4 codec at 400 kbps for video and mp3 codec at constant 32 kbps bitrate for audio:
mencoder -oac mp3lame -lameopts cbr:preset=32 -ovc lavc -lavcopts vbitrate=400 in.avi -o out.avi
* Enhance the sharpness of the video:
mplayer video.avi -vf smartblur=.6:-.5:0,unsharp=l5x5:.8:c5x5:.4
* Merge multiple video into one:
avimerge -i part1.avi part2.avi -o big-file.avi
* Cut a video to keep the first 5.4 seconds:
mencoder big-file.avi -ss 0 -endpos 5.4 -ovc copy -oac copy -o cutted.avi
* Cut a video to keep everything exept the first 5.4 seconds:
mencoder big-file.avi -ss 5.4 -ovc copy -oac copy -o cutted.avi
* Show all mplayer filter list:
mplayer -vf help
* Get help of a particular filter (eq2 in this example):
mplayer -vf eq2=help
* Here is the filter I use to light up a video taken in the dark with my cheap camera. Of course it add noise but thanks to this we can distinguish shapes in the dark.
mencoder -vf eq2=1.61:1.95:0.54:2.43 -oac pcm -ovc lavc video.avi -o bright-vid.avi
* And this is the command to preview the result of the filter used above:
mplayer video.avi -vf eq2=1.61:1.95:0.54:2.43
* This is how I convert raw videos taken with my digital camera into ISO standard MPEG-4 (DivX 5, XVID compatible) videos [to encode in grayscale, add :gray option to -lavcopts]:
mencoder source.avi \
-ovc lavc -oac lavc -ffourcc DX50 \
-lavcopts vcodec=mpeg4:vbitrate=400:v4mv:mbd=2:trell:autoaspect:dia=2:acodec=mp3:abitrate=32:vpass=1 \
-vf hqdn3d -o output.avi
mencoder source.avi \
-ovc lavc -oac lavc -ffourcc DX50 \
-lavcopts vcodec=mpeg4:vbitrate=400:v4mv:mbd=2:trell:autoaspect:dia=2:acodec=mp3:abitrate=32:vpass=2 \
-vf hqdn3d -o output.avi
* Play all videos of the current folder fullscreen at 4x speed with 50% more brightness:
mplayer -speed 4 -brightness 50 -fs ./*.avi
* Extract audio stream from a video:
mplayer -dumpaudio -dumpfile audio.ac3 video_source.mpg
* Test XV video driver output via gstreamer v0.10:
gst-launch-0.10 videotestsrc ! xvimagesink
by
kev
2 years, 3 months ago
in English
* Change the aspect ratio of a film for the playback. Standard aspect ratio are : 1.33 (4:3), 1.66 (1.66:1), 1.77 (16:9) and 2.35 (2.35:1):
mplayer -aspect 2:1 ./video.avi
* Play the video with subtitles:
mplayer -sub ./subtitle_file.txt ./video.avi
* This will extract audio track no. 128, downmix the AC3 sound to PCM and write the results to file.wav:
mplayer -vo null -hardframedrop -aid 128 -ao pcm -aofile file.wav dvd://1
* This will extract the audio, convert it to PCM and write the resulting wave file to audio.wav:
mplayer -vo null -hardframedrop -ao pcm:file=audio.wav myvideo.avi
* Extract to chapter.txt the chapter file of the track n°1 of the DVD:
dvdxchap -t 1 /mnt/cdrom > chapter.txt
* Show all subtitles streams:
mplayer -vo null -ao null -frames 0 -v 2 dvd://1 >&1 | grep sid
* Extract the raw subtitle stream. The -a 0x21 option correspond to the subtitle stream’s hexadecimal number (= 0×20 + id of the stream):
tccat -i /space/st-tng/dic1/ -T 1 -L | tcextract -x ps1 -t vob -a 0x22 > subs-en
* Create a rotated copy of the file.avi video (rotate=1 : clockwise ; rotate=2 : anti-clockwise):
mencoder -vop rotate=2 -oac pcm -ovc lavc ./source.avi -o ./dest.avi
* Preview a video composed of all jpeg files from the current folder at 15fps (mplayer only support jpeg, png, tga and sgi formats):
mplayer "mf://*.jpg" -mf fps=15
* Create a 15fps video from all jpeg files of the current folder:
mencoder "mf://*.jpg" -mf fps=15 -ovc lavc -o ./dest.avi
* Encode a SVCD to AVI file:
mencoder -oac lavc -ovc lavc vcd://1 -o ./svcd.avi
* Transcode video to raw format (be carefull: usually the output video got annoying audio delay):
mencoder -oac pcm -ovc raw -ofps 25 -noskip ./video.wmv -o ./video.avi
* Encode a video using the default mpeg4 codec at 400 kbps for video and mp3 codec at constant 32 kbps bitrate for audio:
mencoder -oac mp3lame -lameopts cbr:preset=32 -ovc lavc -lavcopts vbitrate=400 in.avi -o out.avi
* Enhance the sharpness of the video:
mplayer video.avi -vf smartblur=.6:-.5:0,unsharp=l5x5:.8:c5x5:.4
* Merge multiple video into one:
avimerge -i part1.avi part2.avi -o big-file.avi
* Cut a video to keep the first 5.4 seconds:
mencoder big-file.avi -ss 0 -endpos 5.4 -ovc copy -oac copy -o cutted.avi
* Cut a video to keep everything exept the first 5.4 seconds:
mencoder big-file.avi -ss 5.4 -ovc copy -oac copy -o cutted.avi
* Show all mplayer filter list:
mplayer -vf help
* Get help of a particular filter (eq2 in this example):
mplayer -vf eq2=help
* Here is the filter I use to light up a video taken in the dark with my cheap camera. Of course it add noise but thanks to this we can distinguish shapes in the dark.
mencoder -vf eq2=1.61:1.95:0.54:2.43 -oac pcm -ovc lavc video.avi -o bright-vid.avi
* And this is the command to preview the result of the filter used above:
mplayer video.avi -vf eq2=1.61:1.95:0.54:2.43
* This is how I convert raw videos taken with my digital camera into ISO standard MPEG-4 (DivX 5, XVID compatible) videos [to encode in grayscale, add :gray option to -lavcopts]:
mencoder source.avi \
-ovc lavc -oac lavc -ffourcc DX50 \
-lavcopts vcodec=mpeg4:vbitrate=400:v4mv:mbd=2:trell:autoaspect:dia=2:acodec=mp3:abitrate=32:vpass=1 \
-vf hqdn3d -o output.avi
mencoder source.avi \
-ovc lavc -oac lavc -ffourcc DX50 \
-lavcopts vcodec=mpeg4:vbitrate=400:v4mv:mbd=2:trell:autoaspect:dia=2:acodec=mp3:abitrate=32:vpass=2 \
-vf hqdn3d -o output.avi
* Play all videos of the current folder fullscreen at 4x speed with 50% more brightness:
mplayer -speed 4 -brightness 50 -fs ./*.avi
* Extract audio stream from a video:
mplayer -dumpaudio -dumpfile audio.ac3 video_source.mpg
* Test XV video driver output via gstreamer v0.10:
gst-launch-0.10 videotestsrc ! xvimagesink
mplayer bmovl
mplayer -vf
video filter: bmovl
bmovl=隐藏:不透明:<命名管道>
从一个命名管道读取位图并把它们显示在窗口中.
隐藏: 设置’隐藏’标记的默认值(布尔值)
不透明: 切换alphablended(透明)和不透明(快速)模式标记
命 名管道: 命名管道的路径/文件名(连接mplayer -vf bmovl 和控制程序的命名管道)
命名管道命令有:
RGBA32 width height xpos ypos alpha clear
接受width*height*4字节的原始RGBA32数据
ABGR32 width height xpos ypos alpha clear
接受width*height*4字节的原始ABGR32 data.
RGB24 width height xpos ypos alpha clear
接受width*height*3字节的原始RGB32 data.
BGR24 width height xpos ypos alpha clear
接受width*height*3字节的原始BGR32 data.
ALPHA width height xpos ypos alpha
改变区域的alpha值
CLEAR width height xpos ypos
清除数据
OPAQUE
禁用所有alpha透明发送"ALPHA 0 0 0 0 0"可以重新打开 它.
HIDE
隐藏位图
SHOW
显示位图
参数有:
width, height: 图像/区域尺寸
xpos, ypos: 位图传送的X/Y位置
alpha: 设置alpha差别. 0标识原始值, 255使所有都不透明, -255使所有都透明. 如果你把它设为-255, 你可以随后发 送 一 个ALPHA命令序列吧区域设置为-225, -200, -175等等来获 得一个漂亮的淡入效果! ;)
clear: 传送前清楚帧缓冲. 1表示清除, 如果是0, 图像会被 传送到老图像上, 所以你不需要每次为屏幕小部分的变化都发 送1,8MB的RGBA32数据.
mplayer -vf
Available video filters:
rectangle : draw rectangle
bmovl : Read bitmaps from a FIFO and display them in window
crop : cropping
expand : expanding & osd
pp : postprocessing
scale : software scaling
vo : libvo wrapper
format : force output format
noformat : disallow one output format
yuy2 : fast YV12/Y422p -> YUY2 conversion
flip : flip image upside-down
rgb2bgr : fast 24/32bpp RGB<->BGR conversion
rotate : rotate
mirror : horizontal mirror
palette : 8bpp indexed (using palette) -> BGR 15/16/24/32 conversion
lavc : realtime mpeg1 encoding with libavcodec
lavcdeint : libavcodec's deinterlacing filter
pp7 : postprocess 7
dvbscale : calc Y scaling for DVB card
cropdetect : autodetect crop size
test : test pattern generator
noise : noise generator
yvu9 : fast YVU9->YV12 conversion
eq : soft video equalizer
eq2 : Software equalizer
halfpack : yuv planar 4:2:0 -> packed 4:2:2, half height
dint : drop interlaced frames
1bpp : 1bpp bitmap -> YUV/BGR 8/15/16/32 conversion
2xsai : 2xSai BGR bitmap 2x scaler
unsharp : unsharp mask & gaussian blur
swapuv : UV swapper
il : (de)interleave
fil : fast (de)interleaver
boxblur : box blur
sab : shape adaptive blur
smartblur : smart blur
bmovl
perspective : perspective correcture
down3dright : convert stereo movie from top-bottom to left-right field
field : extract single field
denoise3d : 3D Denoiser (variable lowpass filter)
hqdn3d : High Quality 3D Denoiser
detc : de-telecine filter
telecine : telecine filter
tinterlace : temporal field interlacing
tfields : temporal field separation
ivtc : inverse telecine, take 2
ilpack : 4:2:0 planar -> 4:2:2 packed reinterlacer
dsize : reset displaysize/aspect
decimate : near-duplicate frame remover
softpulldown : mpeg2 soft 3:2 pulldown
pullup : pullup (from field sequence to frames)
filmdint : Advanced inverse telecine filer
framestep : Dump one every n / key frames
tile : Make a single image tiling x/y images
delogo : simple logo remover
remove-logo : Removes a tv logo based on a mask image.
hue : hue changer
spp : simple postprocess
uspp : ultra simple/slow postprocess
fspp : fast simple postprocess
qp : QP changer
mcdeint : motion compensating deinterlacer
geq : generic equation filter
yuvcsp : yuv colorspace converter
kerndeint : Kernel Deinterlacer
rgbtest : rgbtest
phase : phase shift fields
divtc : inverse telecine for deinterlaced video
harddup : resubmit duplicate frames for encoding
softskip : soft (post-filter) frame skipping for encoding
screenshot : screenshot to file
ass : Render ASS/SSA subtitles
yadif : Yet Another DeInterlacing Filter
blackframe : detects black frames
ow : overcomplete wavelet denoiser
video filter: bmovl
bmovl=隐藏:不透明:<命名管道>
从一个命名管道读取位图并把它们显示在窗口中.
隐藏: 设置’隐藏’标记的默认值(布尔值)
不透明: 切换alphablended(透明)和不透明(快速)模式标记
命 名管道: 命名管道的路径/文件名(连接mplayer -vf bmovl 和控制程序的命名管道)
命名管道命令有:
RGBA32 width height xpos ypos alpha clear
接受width*height*4字节的原始RGBA32数据
ABGR32 width height xpos ypos alpha clear
接受width*height*4字节的原始ABGR32 data.
RGB24 width height xpos ypos alpha clear
接受width*height*3字节的原始RGB32 data.
BGR24 width height xpos ypos alpha clear
接受width*height*3字节的原始BGR32 data.
ALPHA width height xpos ypos alpha
改变区域的alpha值
CLEAR width height xpos ypos
清除数据
OPAQUE
禁用所有alpha透明发送"ALPHA 0 0 0 0 0"可以重新打开 它.
HIDE
隐藏位图
SHOW
显示位图
参数有:
width, height: 图像/区域尺寸
xpos, ypos: 位图传送的X/Y位置
alpha: 设置alpha差别. 0标识原始值, 255使所有都不透明, -255使所有都透明. 如果你把它设为-255, 你可以随后发 送 一 个ALPHA命令序列吧区域设置为-225, -200, -175等等来获 得一个漂亮的淡入效果! ;)
clear: 传送前清楚帧缓冲. 1表示清除, 如果是0, 图像会被 传送到老图像上, 所以你不需要每次为屏幕小部分的变化都发 送1,8MB的RGBA32数据.
mplayer -vf
Available video filters:
rectangle : draw rectangle
bmovl : Read bitmaps from a FIFO and display them in window
crop : cropping
expand : expanding & osd
pp : postprocessing
scale : software scaling
vo : libvo wrapper
format : force output format
noformat : disallow one output format
yuy2 : fast YV12/Y422p -> YUY2 conversion
flip : flip image upside-down
rgb2bgr : fast 24/32bpp RGB<->BGR conversion
rotate : rotate
mirror : horizontal mirror
palette : 8bpp indexed (using palette) -> BGR 15/16/24/32 conversion
lavc : realtime mpeg1 encoding with libavcodec
lavcdeint : libavcodec's deinterlacing filter
pp7 : postprocess 7
dvbscale : calc Y scaling for DVB card
cropdetect : autodetect crop size
test : test pattern generator
noise : noise generator
yvu9 : fast YVU9->YV12 conversion
eq : soft video equalizer
eq2 : Software equalizer
halfpack : yuv planar 4:2:0 -> packed 4:2:2, half height
dint : drop interlaced frames
1bpp : 1bpp bitmap -> YUV/BGR 8/15/16/32 conversion
2xsai : 2xSai BGR bitmap 2x scaler
unsharp : unsharp mask & gaussian blur
swapuv : UV swapper
il : (de)interleave
fil : fast (de)interleaver
boxblur : box blur
sab : shape adaptive blur
smartblur : smart blur
bmovl
perspective : perspective correcture
down3dright : convert stereo movie from top-bottom to left-right field
field : extract single field
denoise3d : 3D Denoiser (variable lowpass filter)
hqdn3d : High Quality 3D Denoiser
detc : de-telecine filter
telecine : telecine filter
tinterlace : temporal field interlacing
tfields : temporal field separation
ivtc : inverse telecine, take 2
ilpack : 4:2:0 planar -> 4:2:2 packed reinterlacer
dsize : reset displaysize/aspect
decimate : near-duplicate frame remover
softpulldown : mpeg2 soft 3:2 pulldown
pullup : pullup (from field sequence to frames)
filmdint : Advanced inverse telecine filer
framestep : Dump one every n / key frames
tile : Make a single image tiling x/y images
delogo : simple logo remover
remove-logo : Removes a tv logo based on a mask image.
hue : hue changer
spp : simple postprocess
uspp : ultra simple/slow postprocess
fspp : fast simple postprocess
qp : QP changer
mcdeint : motion compensating deinterlacer
geq : generic equation filter
yuvcsp : yuv colorspace converter
kerndeint : Kernel Deinterlacer
rgbtest : rgbtest
phase : phase shift fields
divtc : inverse telecine for deinterlaced video
harddup : resubmit duplicate frames for encoding
softskip : soft (post-filter) frame skipping for encoding
screenshot : screenshot to file
ass : Render ASS/SSA subtitles
yadif : Yet Another DeInterlacing Filter
blackframe : detects black frames
ow : overcomplete wavelet denoiser
mplayer 半透明 滤镜融合
overlay
http://urandom.ca/mebox/downloads/vf_overlay.txt
mplayer bvmol
http://www.mplayerhq.hu/DOCS/man/zh/mplayer.1.html
官方手册
http://www.mplayerhq.hu/DOCS/HTML-single/zh_CN/MPlayer.html
http://www.mplayerhq.hu/DOCS/man/zh/mplayer.1.html
bmovl=hidden:opaque:fifo
http://www.sit.auckland.ac.nz/Video_overlay_with_mplayer
apt-get install -y xserver-xorg libraw1394-8 libmpeg2-4 mpeg2dec ffmpeg pciutils less xfonts-base vlc coriander libsdl1.2-dev
Various cut and paste command lines.
mplayer -vo x11 -fs -display 0:0 -vf scale=1360:-2 Nosferatu.mp4
mplayer -vo x11 -fs -display 0:0 -vf scale=1024:-2 Nosferatu.mp4
bmovl=hidden:opaque:fifo
-vf bmovl
http://projects.sault.org/mebox/downloads/patches/vf_overlay_outbuf-2008-07-07-TESTING.diff
http://urandom.ca/mebox/downloads/vf_overlay.txt
mplayer bvmol
http://www.mplayerhq.hu/DOCS/man/zh/mplayer.1.html
官方手册
http://www.mplayerhq.hu/DOCS/HTML-single/zh_CN/MPlayer.html
http://www.mplayerhq.hu/DOCS/man/zh/mplayer.1.html
bmovl=hidden:opaque:fifo
http://www.sit.auckland.ac.nz/Video_overlay_with_mplayer
apt-get install -y xserver-xorg libraw1394-8 libmpeg2-4 mpeg2dec ffmpeg pciutils less xfonts-base vlc coriander libsdl1.2-dev
Various cut and paste command lines.
mplayer -vo x11 -fs -display 0:0 -vf scale=1360:-2 Nosferatu.mp4
mplayer -vo x11 -fs -display 0:0 -vf scale=1024:-2 Nosferatu.mp4
bmovl=hidden:opaque:fifo
-vf bmovl
http://projects.sault.org/mebox/downloads/patches/vf_overlay_outbuf-2008-07-07-TESTING.diff
2009年3月1日星期日
可怕的9城服务条款
在注册9诚通行证时,看到以下条款,感觉个人完全没有安全感。
4.1 对于用户所登录留存的个人资料,除下列情形外,第九城市同意在未得到用户同意前,不公开对外披露:
a.基于法律规定
b.基于司法机关或其它有权机关基于法定程序的要求
c.为保障第九城市的财产及权益
d.在紧急情况下为保护其它用户或第三人的人身安全的情形下
4.2 对于用户所登录的个人资料,用户同意第九城市及其关系企业或合作对象,可以在合理范围内搜集、处理、保存、传递及使用该资料,以提供用户其它信息及服务或做成会计资料,或进行网络行为的调查或研究,或其它任何合法使用。
4.3 第九城市无法保证不将用户的个人资料或通讯内容给予第三者。举例来说,如果政府需要资料时第九城市将被迫交出,或者是有其它的第三者拦截网络的传输内容。 此外,用户授权第九城市可将用户的资料给予执法机关或政府相关单位,以利调查或问题解决的进行。除此之外,当用户要求第九城市给予任何技术上的协助时,用 户授权第九城市远程检视与更改用户计算机的内容。为了更新程序的需要,用户授权第九城市(i)从用户的计算机上传档案(ii)下载档案到用户的计算机。
4.1 对于用户所登录留存的个人资料,除下列情形外,第九城市同意在未得到用户同意前,不公开对外披露:
a.基于法律规定
b.基于司法机关或其它有权机关基于法定程序的要求
c.为保障第九城市的财产及权益
d.在紧急情况下为保护其它用户或第三人的人身安全的情形下
4.2 对于用户所登录的个人资料,用户同意第九城市及其关系企业或合作对象,可以在合理范围内搜集、处理、保存、传递及使用该资料,以提供用户其它信息及服务或做成会计资料,或进行网络行为的调查或研究,或其它任何合法使用。
4.3 第九城市无法保证不将用户的个人资料或通讯内容给予第三者。举例来说,如果政府需要资料时第九城市将被迫交出,或者是有其它的第三者拦截网络的传输内容。 此外,用户授权第九城市可将用户的资料给予执法机关或政府相关单位,以利调查或问题解决的进行。除此之外,当用户要求第九城市给予任何技术上的协助时,用 户授权第九城市远程检视与更改用户计算机的内容。为了更新程序的需要,用户授权第九城市(i)从用户的计算机上传档案(ii)下载档案到用户的计算机。
订阅:
评论 (Atom)
