An Analysis of Linux Scalability
- 格式:pdf
- 大小:405.91 KB
- 文档页数:98
U N D E R -C O N S T R A I N E D S Y M B O L I C E X E C U T I O N : C O R R E C T N E S S C H E C K I N G F O R R E A L C O D E 24T H U S E N I X S E C U R I T Y S Y M P O S I U M A U G U S T 12, 2015D A V I D A. R A M O S A N D D A W S O NE N G L E R S TA NF O R D U N I V E R S I T YC O N T R I B U T I O N S•Technique + tool for finding deep bugs in real, open source C/C++ code ‣No manual testcases‣No functional specification•Bugs reported may have security implications; exploitability must bedetermined manually‣Memory access, heap management, assertion failures, division-by-zero •Found 77 new bugs in BIND, OpenSSL, Linux kernel‣ 2 OpenSSL DoS vulnerabilities: CVE-2014-0198, CVE-2015-0292‣14 Linux kernel vulnerabilities (mostly minor DoS issues)M O T I VAT I O N:C U R R E N T P R A C T I C E •Code reviews•“Safer” languages•Manual (regression) testing•Static analysis (Coverity, clang static analyzer, etc.)Bugs are everywhere!S Y M B O L I C E X E C U T I O N•Provide symbolic rather than concrete inputs •Conceptually: explore all paths through a program •Accurately track all memory values (bit precision) •Report paths/inputs that crash‣Generate concrete testcase•KLEE tool (prior work: OSDI 2008)x is symbolic input int foo(int x) {if (x)return x/10;elsereturn 10/x;}int foo(int x) {symbolic branch if (x)return x/10;elsereturn 10/x;}if (x)return x/10;elsereturn 10/x;x != 0x == 0 State 1State 2 Division: OKDivision: ERRORP R O B L E M:S C A L A B I L I T Y•Path explosion‣| paths | ~ 2 | if-statements |•Path length and complexity‣Undecidable: infinite-length paths (halting problem) •SMT query complexity (NP-complete)S O L U T I O N:U N D E R-C O N S T R A I N E D •Directly execute individual functions within a program ‣Less code = Fewer paths‣Function calls executed (inter-procedural)‣Able to test previously-unreachable code •Challenges‣Complex inputs (e.g., pointer-rich data structures)‣Under-constrained: inputs have unknown preconditions-False positivesU C-K L E E T O O L•Extends KLEE tool (OSDI 2008)•Runs LLVM bitcode compiled from C/C++ source •Automatically synthesizes complex inputs‣Based on lazy initialization (Java PathFinder)‣Supports pointer manipulation and casting in C/C++ (no type safety) ‣User-specified input depth (k-bound) [Deng 2006]L A Z Y I N I T I A L I Z AT I O N •Symbolic (input) pointers initially unbound •On first dereference:‣New object allocated‣Symbolic pointer bound to new object’s address‣Assume no aliasing (i.e., no cyclical data structures) •On subsequent dereferences:‣Pointer resolves to object allocated aboveE X A M P L E unbound symbolic inputint listSum(node *n) {int sum = 0;while (n) {sum += n->val;n = n->next;}return sum;}int listSum(node *n) { int sum = 0;while (n) {sum += n->val;n = n->next;}return sum;}int listSum(node *n) { int sum = 0;while (n) {sum += n->val;n = n->next;}return sum;}int listSum(node *n) { int sum = 0;while (n) {sum += n->val; n = n->next; }return sum;}n n != 0int listSum(node *n) { int sum = 0; while (n) {sum += n->val; n = n->next; }return sum;}nn != 0int listSum(node *n) { int sum = 0; while (n) {sum += n->val; n = n->next; }return sum;}nn != 0uc_node1nextval n == &uc_node1int listSum(node *n) { int sum = 0; while (n) {sum += n->val;n = n->next; }return sum;}nn != 0uc_node1nextval n == &uc_node1int listSum(node *n) { int sum = 0;while (n) {sum += n->val; n = n->next; }return sum;}nn != 0uc_node1nextvaln == &uc_node1int listSum(node *n) { int sum = 0;while (n) {sum += n->val; n = n->next; }return sum;}nn != 0uc_node1nextval n == &uc_node1nn != 0uc_node1nextval n == &uc_node1N U L Luc_node1.next == 0int listSum(node *n) { int sum = 0; while (n) {sum += n->val; n = n->next; }return sum;}nn != 0uc_node1nextval n == &uc_node1nn != 0uc_node1nextval n == &uc_node1N U L Luc_node1.next == 0int listSum(node *n) { int sum = 0; while (n) {sum += n->val; n = n->next; }return sum;}nn != 0uc_node1nextval n == &uc_node1nn != 0uc_node1nextval n == &uc_node1N U L Luc_node2nextval uc_node1.next == &uc_node2uc_node1.next == 0int listSum(node *n) { int sum = 0; while (n) {sum += n->val;n = n->next; }return sum;}nn != 0uc_node1nextval n == &uc_node1nn != 0uc_node1nextval n == &uc_node1N U L Luc_node2nextval uc_node1.next == &uc_node2uc_node1.next == 0int listSum(node *n) { int sum = 0;while (n) {sum += n->val; n = n->next; }return sum;}nn != 0uc_node1nextval n == &uc_node1nn != 0uc_node1nextval n == &uc_node1N U L Luc_node2nextval uc_node1.next == &uc_node2uc_node1.next == 0int listSum(node *n) { int sum = 0;while (n) {sum += n->val; n = n->next; }return sum;}nn != 0uc_node1nextval n == &uc_node1nn != 0uc_node1nextval n == &uc_node1N U L Luc_node2nextval N U L Luc_node1.next == &uc_node2uc_node2.next == 0uc_node1.next == 0int listSum(node *n) { int sum = 0; while (n) {sum += n->val; n = n->next; }return sum;}nn != 0uc_node1nextval n == &uc_node1nn != 0uc_node1nextval n == &uc_node1N U L Luc_node2nextval N U L Luc_node1.next == &uc_node2uc_node2.next == 0uc_node1.next == 0U S E C A S E S•Equivalence checking: patches‣Yesterday’s code vs. today’s code (i.e., fewer bugs today)‣Goal: detect (and prevent!) new crashes introduced by patches ‣Other uses discussed in CAV 2011 paperPAT C H E SSource: https:///phabricatorU S E C A S E S•Equivalence checking: patches‣Yesterday’s code vs. today’s code (i.e., fewer bugs today)‣Goal: detect (and prevent!) new crashes introduced by patches‣Other uses discussed in CAV 2011 paper•General bug-finding: rule-based checkers‣Single version of a function; under-constrained + additional checker rules ‣Memory leaks, uninitialized data, unsafe user input‣Simple interface for adding new checkersretA = fooA(x);retB = fooB(x);assert(retA == retB);identical input (symbolic)assert equivalence•Value equivalence‣Return value‣Arguments passed by reference‣Global/static variables‣System call effects (modeled) •Error (crash) equivalence‣Both versions typically have the same same (unknown) preconditions!‣Neither version crashes on an input‣Both versions crash on an inputUSE CASE: whether patches introduce crashes•Check per path equivalence of two functions•If all paths exhausted, equivalence verified (up to input bound)E VA L U AT I O N•BIND, OpenSSL‣Mature, security-critical codebases (~400 KLOC each) •Patches‣BIND: 487 patches to 9.9 stable (14 months)‣OpenSSL: 324 patches to 1.0.1 stable (27 months) •Ran UC-KLEE for 1 hour on each patched functionE VA L U AT I O N:PAT C H E S•Discovered 10 new bugs (4 in BIND, 6 in OpenSSL)‣ 2 OpenSSL DoS vulnerabilities:-CVE-2014-0198: NULL pointer dereference-CVE-2015-0292: Out-of-bounds memcpy read•Verified (w/ caveats) that patches do not introduce crashes ‣67 (13.8%) for BIND, 48 (14.8%) for OpenSSL‣Caveat: max. input size (25KB), tool limitations/bugsdo_ssl3_write():1 if (wb->buf == NULL)2 if (!ssl3_setup_write_buffer(s))3 return -1;4 ...5 /* If we have an alert to send, lets send it */6 if (s->s3->alert_dispatch) {7 i=s->method->ssl_dispatch_alert(s);8 if (i <= 0)9 return (i);10 /* if it went, fall through and send more stuff */11 }12 ...13 unsigned char *p = wb->buf ;14 *(p++)=type&0xff;NULL pointer check call sets wb->buf to NULL NULL pointer dereference•Uncommon code path‣SSL_MODE_RELEASE_BUFFERS runtime option (used by Apache mod_ssl) ‣SSL alert pending (could be triggered by attacker)‣Difficult to consider this case with traditional testingFA L S E P O S I T I V E S•Function’s inputs have unknown preconditions •Partial solutions‣Automated heuristics‣Manual annotations (lazily, as needed)-Written in C/C++, separate from codebase-Simple annotation can silence many errorsFA L S E P O S I T I V E S:E X A M P L E(B I N D)1 int isc_region_compare(isc_region_t *r1, isc_region_t *r2) {2 unsigned int l;3 int result;45 REQUIRE(r1 != NULL);6 REQUIRE(r2 != NULL);78 l = (r1->length < r2->length) ? r1->length : r2->length;910 if ((result = memcmp(r1->base, r2->base, l)) != 0)11 return ((result < 0) ? -1 : 1);12 else13 return ((r1->length == r2->length) ? 0 :14 (r1->length < r2->length) ? -1 : 1);15 }INVARIANT(r->length <= OBJECT_SIZE(r->base));623 errors silenced (7.5% of all errors reported for BIND)M A N U A L A N N O TAT I O N S•BIND: 400 lines of annotation code (~0.1%)•OpenSSL: 60 lines of annotation code (~0.02%) •Reasonable effort relative to code size (~400 KLOC) and importanceG E N E R A L B U G-F I N D I N G•Run single version of a function (w/ lazy initialization) •Individual checkers look for specific types of bugs:‣Leak checker‣Uninitialized data checker‣User input checker•Like Valgrind but applied to all execution pathsE VA L U AT I O N•20,000+ functions: BIND, OpenSSL, Linux kernel (~12 MLOC) •Found 67 new bugs‣37 memory leaks-Linux kernel: exploitable AUTH_GSS leak in NFS SunRPC layer‣19 uses of uninitialized data-BIND: DNS UDP port PRNG selected by uninitialized value-Linux kernel: leak of private kernel stack data via firewire ioctl‣11 unsafe user input (Linux kernel only)-VMware VMCI driver: unchecked memcpy length (~Heartbleed)-CEPH distributed file system: division-by-zero (kernel FPE)U S E R I N P U T C H E C K E R•User input is fully-constrained (an attacker may supply any value); no unknown input preconditions•Checker tracks whether each symbolic byte is UC/FC •Checker emits UNSAFE_INPUT flag if error is caused by FC input •Suppresses flag for inputs possibly sanitized (false pos. trade-off) •C annotations: specify functions returning user input‣Linux: get_user, copy_from_user, syscall args‣BIND: isc_buffer_getuint8‣OpenSSL: byte-swaps (n2s, n2l, etc.) [Chou]K E R N E L V M C I V U L N E R A B I L I T Y1 static int dg_dispatch_as_host(...,2 struct vmci_datagram *dg) {3 dg_size = VMCI_DG_SIZE(dg);4 ...5 dg_info = kmalloc(sizeof (*dg_info) +6 (size_t) dg->payload_size, GFP_ATOMIC);7 ...8 memcpy(&dg_info->msg, dg, dg_size);9 ...10 }copy_from_user()Fully constrained Unchecked memcpy lengthSend up to 69,632 bytes from host private kernel memory to guest OSSimilar to Heartbleed! (much lower impact)C O N C L U S I O N•Under-constrained symbolic execution •Equivalence checking: patches•General bug-finding: rule-based checkers •Experimental results: BIND, OpenSSL, Linux kernel@ramosbugsQ U E S T I O N S ?。
嵌入式linux常见评估指标介绍在嵌入式项目预研前期阶段,我们常常需要对某个平台进行资源和性能方面的评估,以下是最常见的一些评估指标:1、内存评估系统内存空间通过free、cat /proc/meminfo或者top,查看内存情况。
一般有这样一个经验公式:应用程序可用内存/系统物理内存>70%时,表示系统内存资源非常充足,不影响系统性能;20%<应用程序可用内存/系统物理内存<70%时,表示系统内存资源基本能满足应用需求,暂时不影响系统性能;应用程序可用内存/系统物理内存<20%时,表示系统内存资源紧缺,需要增加系统内存;$ freetotal used free shared buff/c ac heav ai lableMem: 123496 21512 75132 1132 26852 63416Swap: 0 0 0$ cat /proc/meminfoMemTotal: 123496 kB //所有可用的内存大小,物理内存减去预留位和内核使用。
系统从加电开始到引导完成,firmware/B IOS要预留一些内存,内核本身要占用一些内存,最后剩下可供内核支配的内存就是MemTotal。
这个值在系统运行期间一般是固定不变的,重启会改变。
MemFree: 75132 kB //表示系统尚未使用的内存。
MemAvailable: 63400 kB //真正的系统可用内存,系统中有些内存虽然已被使用但是可以回收的,比如cache/buffer、slab都有一部分可以回收,所以这部分可回收的内存加上MemFree才是系统可用的内存Buffe rs: 5644 kB //用来给块设备做缓存的内存,(文件系统的met ad ata、pages)Cached: 19040 kB //分配给文件缓冲区的内存,例如vi一个文件,就会将未保存的内容写到该缓冲区SwapCached: 0 kB //被高速缓冲存储用的交换空间(硬盘的swap)的大小Active: 20356 kB //经常使用的高速缓冲存储器页面文件大小Inactive: 12628 kB //不经常使用的高速缓冲存储器文件大小Active(anon): 9412 kB //活跃的匿名内存Inactive(anon): 20 kB //不活跃的匿名内存Active(file): 10944 kB //活跃的文件使用内存Inactive(file): 12608 kB //不活跃的文件使用内存Unevictable: 0 kB //不能被释放的内存页Mlocked: 0 kB //系统调用 mlockSwapTotal: 0 kB //交换空间总内存SwapFree: 0 kB //交换空间空闲内存Dirty: 0 kB //等待被写回到磁盘的Wri te back: 0 kB //正在被写回的AnonPages: 8300 kB //未映射页的内存/映射到用户空间的非文件页表大小Mapped: 11480 kB //映射文件内存Shmem: 1132 kB //已经被分配的共享内存KReclaimable: 2132 kB //内核内存,内存压力时内核尝试回收Slab: 8240 kB //内核数据结构缓存SReclaimable: 2132 kB //可收回slab内存SUnreclaim: 6108 kB //不可收回slab内存KernelStack: 568 kB //内核消耗的内存PageTables: 516 kB //管理内存分页的索引表的大小NFS_Unstable: 0 kB //不稳定页表的大小Bounce: 0 kB //在低端内存中分配一个临时buffer作为跳转,把位于高端内存的缓存数据复制到此处消耗的内存WritebackTmp: 0 kB //FUSE用于临时写回缓冲区的内存CommitLimit: 61748 kB //系统实际可分配内存Committed_AS: 58568 kB //系统当前已分配的内存VmallocTotal: 1048372 kB //预留的虚拟内存总量VmallocUsed: 1288 kB //已经被使用的虚拟内存VmallocChunk: 0 kB //可分配的最大的逻辑连续的虚拟内存Per cpu: 32 kB //percpu机制使用的内存2、磁盘评估获取磁盘空间$ df -hFilesystem Size Used Available Use% Mounted on /dev/root 6.0M 6.0M 0 100% /romtmpfs 60.3M 1.1M 59.2M 2% /tmp/dev/mtdblock6 23.8M 9.0M 14.8M 38% /overlay overlayfs:/overlay 23.8M 9.0M 14.8M 38% /tmpfs 512.0K 0 512.0K 0% /dev Filesystem:代表该文件系统时哪个分区,所以列出的是设备名称。
外文资料原文JSPJSP (JavaServer Pages) is initiated by Sun Microsystems, Inc., with many companies to participate in the establishment of a dynamic web page technical standards. JSP technology somewhat similar to ASP technology, it is in the traditional HTML web page document (*. htm, *. html) to insert the Java programming paragraph (Scriptlet) and JSP tag (tag), thus JSP documents (*. jsp).Using JSP development of the Web application is cross-platform that can run on Linux, is also available for other operating systems.JSP technology to use the Java programming language prepared by the category of XML tags and scriptlets, to produce dynamic pages package processing logic. Page also visit by tags and scriptlets exist in the services side of the resources of logic. JSP page logic and web page design and display separation, support reusable component-based design, Web-based application development is rapid and easy.Web server in the face of visits JSP page request, the first implementation of the procedures of, and then together with the results of the implementation of JSP documents in HTML code with the return to the customer. Insert the Java programming operation of the database can be re-oriented websites, in order to achieve the establishment of dynamic pages needed to function.JSP and Java Servlet, is in the implementation of the server, usually returned to the client is an HTML text, as long as the client browser will be able to visit.JSP 1.0 specification of the final version is launched in September 1999, December has introduced 1.1 specifications. At present relatively new is JSP1.2 norms, JSP2.0 norms of the draft has also been introduced.JSP pages from HTML code and Java code embedded in one of the components. The server was in the pages of client requests after the Java code and then will generate the HTML pages to return to the client browser. Java Servlet JSP is the technical foundation and large-scale Web application development needs of Java Servlet and JSP support to complete. JSP with the Java technology easy to use, fully object-oriented, and a platform-independent and secure, mainly for all the characteristics of the Internet.JavaScript, which is completely distinct from the Java programming language, is normally used to dynamically generate HTML on the client, building parts of the Web page as the browser loads the document. This is a useful capability and does not normally overlap with the capabilities of JSP (which runs only on the server). JSP pages still include SCRIPT tags for JavaScript, just asnormal HTML pages do. In fact, JSP can even be used to dynamically generate the JavaScript that will be sent to the client. So, JavaScript is not a competing technology; it is a complementary one.It is also possible to use JavaScript on the server, most notably on Sun ONE (formerly iPlanet), IIS, and BroadVision servers. However, Java is more powerful, flexible, reliable, and portable.JSP (a recursive acronym for "JSP: Hypertext Preprocessor") is a free, open-source, HTML-embedded scripting language that is somewhat similar to both ASP and JSP. One advantage of JSP is that the dynamic part is written in Java, which already has an extensive API for networking, database access, distributed objects, and the like, whereas PHP requires learning an entirely new, less widely used language. A second advantage is that JSP is much more widely supported by tool and server vendors than is JSP.Versus Pure Servlets.JSP doesn't provide any capabilities that couldn't, in principle, be accomplished with servlets. In fact, JSP documents are automatically translated into servlets behind the scenes. But it is more convenient to write (and to modify!) regular HTML than to use a zillion println statements to generate the HTML. Plus, by separating the presentation from the content, you can put different people on different tasks: your Web page design experts can build the HTML by using familiar tools and either leave places for your servlet programmers to insert the dynamic content or invoke the dynamic content indirectly by means of XML tags.Does this mean that you can just learn JSP and forget about servlets? Absolutely not! JSP developers need to know servlets for four reasons:a.JSP pages get translated into servlets. You can't understand how JSP works without understanding servlets.b.JSP consists of static HTML, special-purpose JSP tags, and Java code. What kind of Java code? Servlet code! You can't write that code if you don't understand servlet programming.c.Some tasks are better accomplished by servlets than by JSP. JSP is good at generating pages that consist of large sections of fairly well structured HTML or other character data. Servlets are better for generating binary data, building pages with highly variable structure, and performing tasks (such as redirection) that involve little or no output.d.Some tasks are better accomplished by a combination of servlets and JSP than by either servlets or JSP alone.JSP technology strength(1)time to prepare, run everywhere. At this point Java better than PHP, in addition to systems, the code not to make any changes.(2)the multi-platform support. Basically on all platforms of any development environment, in any environment for deployment in any environment in the expansion. Compared ASP / PHP limitations are obvious.(3) a strong scalability. From only a small Jar documents can run Servlet JSP, to the multiple servers clustering and load balancing, to multiple Application for transaction processing, information processing, a server to numerous servers, Java shows a tremendous Vitality.(4)diversification and powerful development tools support. This is similar to the ASP, Java already have many very good development tools, and many can be free, and many of them have been able to run on a variety of platforms under.Versus JavaScriptJavaScript, which is completely distinct from the Java programming language, is normally used to dynamically generate HTML on the client, building parts of the Web page as the browser loads the document. This is a useful capability and does not normally overlap with the capabilities of JSP (which runs only on the server). JSP pages still include SCRIPT tags for JavaScript, just as normal HTML pages do. In fact, JSP can even be used to dynamically generate the JavaScript that will be sent to the client. So, JavaScript is not a competing technology; it is a complementary one.外文资料译文JSPJSP(JavaServer Pages)是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。
Projects: Linux scalability: Accept() scalability on LinuxMacro-BenchmarkThe results of the macro-benchmark are very encouraging. While running with a stable load of anywhere between 100 and 1500 simultaneous connections to the web server, the number of requests serviced per second increased dramatically with both the "wake one" and "task exclusive" patches. While the performance impact is not as powerful as that evidenced in the micro-benchmark, a considerable gain is evident in the testing. Whether the number of simultaneous connections is at a low level, or reaching the upper bounds of the test, the performance increase due to either patch remains steady at just over 50%. There is no discernable difference between the two patches.ConclusionBy thoroughly studying this "thundering herd" problem, we have shown that it is indeed a bottleneck in high-load server performance, and that either patch significantly improves the performance of a high-load server. Even though both patches performed well in the testing, the "wake one" patch is cleaner and easier to incorporate into new or existing code. It also has the advantage of not committing a task to "exclusive" status before it is awakened, so extra code doesn't have to be incorporated for special cases to completely empty the wait-queue. The "wake one" patch can also solve any "thundering herd" problems locally, while the "task exclusive" method may require changes in multiple places where the programmer is responsible for making sure that all adjustments are made. This makes the "wake one" solution easily extensible to all parts of thefor info, email info@ or call +1 (734) 763-2929. Copyright © 1999 University of Michigan Regents. All rightsreserved.。
中图分类号:TP316.81文献标识码:A文章编号:1009-2552(2007)12-0050-03日志技术在Linux文件系统中的研究与应用戴彤彤,刘胜辉,王磊(哈尔滨理工大学计算机科学与技术学院,哈尔滨150080)摘要:介绍了日志技术的主要特点,详细讲解了一种最常用的日志技术形式,同时针对日志技术在Linux文件系统中的应用作了深入的分析,并对ReiserFS、JFS、XFS几种主要日志文件系统进行了分析和研究。
关键词:Linux文件系统;日志;ReiserFS;JFS;XFSAnalysis of Linux file system using log technologyDAI Tong-tong,LI U Sheng-hui,WANG Lei(School of Computer Science and Technology,Harbin University of Science andTechnology,Harbin150080,China)Abstract:This paper analyses characteristics of log technology and the most popular method of using it.It discusses on some problems of log technology applying into Linux file syste m in detail,and researches several kinds of journaling file system,such as ReiserFS,JFS and XFS.Key w ords:Linux file syste m;Log;ReiserFS;JFS;XFS0引言文件系统是Linux操作系统中重要的组成部分,它是操作系统在计算机的硬盘上存储和检索数据的逻辑方法,是系统中数据信息的管理组织形式。
About the T utorialScala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. Scala has been created by Martin Odersky and he released the first version in 2003.Scala smoothly integrates the features of object-oriented and functional languages. This tutorial explains the basics of Scala in a simple and reader-friendly way.AudienceThis tutorial has been prepared for beginners to help them understand the basics of Scala in simple and easy steps. After completing this tutorial, you will find yourself at a moderate level of expertise in using Scala from where you can take yourself to next levels. PrerequisitesScala Programming is based on Java, so if you are aware of Java syntax, then it's pretty easy to learn Scala. Further if you do not have expertise in Java but if you know any other programming language like C, C++ or Python then it will also help in grasping Scala concepts very quickly.Disclaimer & Copyright© Copyright 2015 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher.We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or inthistutorial,******************************************.iT able of ContentsAbout this Tutorial (i)Audience (i)Prerequisites (i)Disclaimer & Copyright (i)Table of Contents .................................................................................................................................. i i 1.SCALA – OVERVIEW (1)Scala vs Java (2)Scala Web Frameworks (2)2.SCALA – ENVIRONMENT (3)Step 1: Verify your Java Installation (3)Step 2: Set your Java Environment (4)Step 3: Install Scala (4)3.SCALA – BASICS (7)First Scala Program (7)Script Mode (8)Basic Syntax (9)Scala Identifiers (9)Scala Keywords (10)Comments in Scala (11)Blank Lines and Whitespace (11)Newline Characters (12)Scala Packages (12)Apply Dynamic (12)ii4.SCALA – DATA (14)Scala Basic Literals (14)Escape Sequences (16)5.SCALA – VARIABLES (18)Variable Declaration (18)Variable Data Types (18)Variable Type Inference (19)Multiple assignments (19)Example Program (19)Variable Scope (20)6.SCALA – CLASSES & OBJECTS (22)Basic Class (22)Extending a class (24)Implicit Classes (26)Singleton Objects (28)7.SCALA – ACCESS MODIFIERS (30)Private Members (30)Protected Members (30)Public Members (31)Scope of Protection (32)8.SCALA – OPERATORS (34)Arithmetic Operators (34)Relational Operators (35)Logical Operators (37)Bitwise Operators (39)iiiOperators Precedence in Scala (46)9.SCALA – IF ELSE STATEMENT (48)if Statement (48)If-else Statement (49)If-else-if-else Statement (50)Nested if-else Statement (52)10.SCALA – LOOP STATEMENTS (54)While loop (55)do-while loop (57)for Loop (59)Loop Control Statements (66)Break Statement (66)Breaking Nested Loops (68)The infinite Loop (70)11.SCALA – FUNCTIONS (72)Function Declarations (72)Function Definitions (72)Calling Functions (73)Function Call-by-Name (74)Function with Variable Arguments (75)Function Default parameter values (76)Nested Functions (77)Partially Applied Functions (78)Function with Named arguments (80)Recursion Functions (81)ivAnonymous Functions (83)Currying Functions (84)12.SCALA – CLOSURES (86)13.SCALA – STRINGS (88)Creating a String (88)String Length (89)Concatenating Strings (89)Creating Format Strings (90)String Interpolation (91)The ‘f’ Interpolator (92)String Methods (94)14.SCALA – ARRAYS (98)Declaring Array Variables (98)Processing Arrays (99)Multi-Dimensional Arrays (100)Concatenate Arrays (102)Create Array with Range (103)Scala Array Methods (104)15.SCALA – COLLECTIONS (106)Scala Lists (106)Creating Uniform Lists (109)Tabulating a Function (110)Scala List Methods (111)Scala Sets (114)vFind max, min elements in set (117)Find Common Values Insets (118)Scala Map [K, V] (122)Concatenating Maps (124)Print Keys and Values from a Map (125)Check for a key in Map (126)Scala Map Methods (127)Scala Tuples (131)Iterate over the Tuple (132)Converting to String (133)Scala Options (134)Using getOrElse() Method (136)Using isEmpty() Method (137)Scala Option Methods (137)Scala Iterators (139)Find Min & Max values Element (140)Find the length of the Iterator (140)Scala Iterator Methods (141)16.SCALA – TRAITS (146)Value classes and Universal traits (148)When to Use Traits? (148)17.SCALA – PATTERN MATCHING (150)Matching using case Classes (151)18.SCALA – REGULAR EXPRESSIONNS (154)Forming Regular Expressions (156)vi19.SCALA – EXCEPTION HANDLING (161)Throwing Exceptions (161)Catching Exceptions (161)The finally Clause (162)20.SCALA – EXTRACTORS (164)Example (164)Pattern Matching with Extractors (165)21.SCALA – FILES I/O (167)Reading a Line from Command Line (167)Reading File Content (168)vii1.Scala, short for Scalable Language, is a hybrid functional programming language. It was created by Martin Odersky. Scala smoothly integrates the features of object-oriented and functional languages. Scala is compiled to run on the Java Virtual Machine. Many existing companies, who depend on Java for business critical applications, are turning to Scala to boost their development productivity, applications scalability and overall reliability.Here we have presented a few points that makes Scala the first choice of application developers.Scala is object-orientedScala is a pure object-oriented language in the sense that every value is an object. Types and behavior of objects are described by classes and traits which will be explained in subsequent chapters.Classes are extended by subclassing and a flexible Mixin-based composition mechanism as a clean replacement for multiple inheritance.Scala is functionalScala is also a functional language in the sense that every function is a value and every value is an object so ultimately every function is an object.Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and supports currying functions. These concepts will be explained in subsequent chapters.Scala is statically typedScala, unlike some of the other statically typed languages (C, Pascal, Rust, etc.), does not expect you to provide redundant type information. You don't have to specify a type in most cases, and you certainly don't have to repeat it.Scala runs on the JVMScala is compiled into Java Byte Code which is executed by the Java Virtual Machine (JVM). This means that Scala and Java have a common runtime platform. You can easily move from Java to Scala.The Scala compiler compiles your Scala code into Java Byte Code, which can then be executed by the ‘scala’ command. The ‘s cala’ command is similar to the java command, in that it executes your compiled Scala code.Scala Scala can Execute Java CodeScala enables you to use all the classes of the Java SDK and also your own custom Java classes, or your favorite Java open source projects.Scala can do Concurrent & Synchronize processingScala allows you to express general programming patterns in an effective way. It reduces the number of lines and helps the programmer to code in a type-safe way. It allows you to write codes in an immutable manner, which makes it easy to apply concurrency and parallelism (Synchronize).Scala vs JavaScala has a set of features that completely differ from Java. Some of these are: ∙All types are objects∙Type inference∙Nested Functions∙Functions are objects∙Domain specific language (DSL) support∙Traits∙Closures∙Concurrency support inspired by ErlangScala Web FrameworksScala is being used everywhere and importantly in enterprise web applications. You can check a few of the most popular Scala web frameworks:∙The Lift Framework∙The Play framework∙The Bowler framework2Scala3Scala can be installed on any UNIX flavored or Windows based system. Before you start installing Scala on your machine, you must have Java 1.8 or greater installed on your computer.Follow the steps given below to install Scala.Step 1: V erify Y our Java InstallationFirst of all, you need to have Java Software Development Kit (SDK) installed on your system. To verify this, execute any of the following two commands depending on the platform you are working on.If the Java installation has been done properly, then it will display the current version and specification of your Java installation. A sample output is given in the following table. PlatformCommandSample OutputWindowsOpen Command Console and type:\>java –versionJava version "1.8.0_31" Java (TM) SE Run TimeEnvironment (build 1.8.0_31-b31) Java Hotspot (TM) 64-bit Server VM (build 25.31-b07, mixed mode)LinuxOpen Command terminal and type: $java –versionJava version "1.8.0_31"Open JDK Runtime Environment (rhel-2.8.10.4.el6_4-x86_64)Open JDK 64-Bit Server VM (build 25.31-b07, mixed mode)2.We assume that the readers of this tutorial have Java SDK version 1.8.0_31 installed on their system.In case you do not have Java SDK, download its current version from /technetwork/java/javase/downloads/index.html and install it.Step 2: Set Y our Java EnvironmentSet the environment variable JAVA_HOME to point to the base directory location where Java is installed on your machine. For example,Platform DescriptionWindows Set JAVA_HOME to C:\ProgramFiles\java\jdk1.7.0_60Linux Export JAVA_HOME=/usr/local/java-currentAppend the full path of Java compiler location to the System Path.Platform DescriptionWindows Append the String "C:\Program Files\Java\jdk1.7.0_60\bin" to the end of the system variable PATH.Linux Export PATH=$PATH:$JAVA_HOME/bin/Execute the command java -version from the command prompt as explained above.Step 3: Install ScalaYou can download Scala from /downloads. At the time of writing this tutorial, I downloaded ‘scala-2.11.5-installer.jar’. Make sure you have admin privilege to proceed. Now, execute the following command at the command prompt:Platform Command & Output Description4Windows\>java –jar scala-2.11.5-installer.jar\> This command will display an installation wizard, which will guide you to install Scala on your windows machine. During installation, it will ask for license agreement, simply accept it and further it will ask a path where Scala will be installed. I selected default given path “C:\Program Files\Scala”, you can select a suitable path as per your convenience.Linux Command:$java –jar scala-2.9.0.1-installer.jarOutput:Welcome to the installation of Scala2.9.0.1!The homepage is at: http://Scala-/press 1 to continue, 2 to quit, 3 toredisplay1 ................................................[ Starting to unpack ][ Processing package: Software PackageInstallation (1/1) ][ Unpacking finished ][ Console installation done ]During installation, it will ask forlicense agreement, to accept ittype 1 and it will ask a path whereScala will be installed. I entered/usr/local/share, you can select asuitable path as per yourconvenience.Finally, open a new command prompt and type Scala -version and press Enter. You should see the following:Platform Command Output5Windows \>scala -version Scala code runner version 2.11.5 -- Copyright 2002-2013, LAMP/EPFLLinux $scala -version Scala code runner version2.9.0.1 – Copyright 2002-2013, LAMP/EPFL6Scala7If you have a good understanding on Java, then it will be very easy for you to learn Scala. The biggest syntactic difference between Scala and Java is that the ‘;’ line end character is optional.When we consider a Scala program, it can be defined as a collection of objects that communicate via invoking each other’s methods. Let us now briefly look into what do class, object, methods and instant variables mean.∙Object - Objects have states and behaviors. An object is an instance of a class. Example: A dog has states - color, name, breed as well as behaviors - wagging, barking, and eating.∙Class - A class can be defined as a template/blueprint that describes the behaviors/states that object of its type support.∙Methods - A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.∙Fields - Each object has its unique set of instant variables, which are called fields. An object's state is created by the values assigned to these fields.∙Closure - A closure is a function, whose return value depends on the value of one or more variables declared outside this function.∙Traits - A trait encapsulates method and field definitions, which can then be reused by mixing them into classes. Traits are used to define object types by specifying the signature of the supported methods.First Scala ProgramWe can execute a Scala program in two modes: one is interactive mode and another is script mode .Interactive ModeOpen the command prompt and use the following command to open Scala. \>ScalaIf Scala is installed in your system, the following output will be displayed:3.Welcome to Scala version 2.9.0.1Type in expressions to have them evaluated.Type: help for more information.Type the following text to the right of the Scala prompt and press the Enter key:Scala> println(“Hello, scala”);It will produce the following result:Hello, Scala!Script ModeUse the following instructions to write a Scala program in script mode. Open notepad and add the following code into it.object HelloWorld {/* This is my first java program.* This will print 'Hello World' as the output*/def main(args: Array[String]) {println("Hello, world!") // prints Hello World}}Save the file as: HelloWorld.scala.Open the command prompt window and go to the directory where the program file is saved. The ‘scalac’ command is used to compile the Scala program and it will generate a few class files in the current directory. One of them will be called HelloWorld.class. This is a bytecode which will run on Java Virtual Machine (JVM) using ‘scala’ command.Use the following command to compile and execute your Scala program.\>scalac HelloWorld.scala\>scala HelloWorldOutput:8Hello, World!Basic SyntaxThe following are the basic syntaxes and coding conventions in Scala programming.∙Case Sensitivity - Scala is case-sensitive, which means identifier Hello and hello would have different meaning in Scala.∙Class Names - For all class names, the first letter should be in Upper Case.If several words are used to form a name of the class, each inner word's first letter should be in Upper Case. Example: class MyFirstScalaClass.∙Method Names - All method names should start with a Lower Case letter.If multiple words are used to form the name of the method, then each inner word's first letter should be in Upper Case. Example: def myMethodName()∙Program File Name - Name of the program file should exactly match the object name. When saving the file you should save it using the object name (Remember Scala is case-sensitive) and append ‘.scala’ to the end of the name. (If the file name and the object name do not match your program will not compile).Example:Assume 'HelloWorld' is the object name. Then the file should be saved as 'HelloWorld.scala'.∙def main(args: Array[String]) - Scala program processing starts from the main() method which is a mandatory part of every Scala Program.Scala IdentifiersAll Scala components require names. Names used for objects, classes, variables and methods are called identifiers. A keyword cannot be used as an identifier and identifiers are case-sensitive. Scala supports four types of identifiers.Alphanumeric IdentifiersAn alphanumeric identifier starts with a letter or an underscore, which can be followed by further letters, digits, or underscores. The '$' character is a reserved keyword in Scala and should not be used in identifiers.Following are legal alphanumeric identifiers:age, salary, _value, __1_valueFollowing are illegal identifiers:9$salary, 123abc, -salaryOperator IdentifiersAn operator identifier consists of one or more operator characters. Operator characters are printable ASCII characters such as +, :, ?, ~ or #.Following are legal operator identifiers:+, ++, :::, <?>, :>,The Scala compiler will internally "mangle" operator identifiers to turn them into legal Java identifiers with embedded $ characters. For instance, the identifier :-> would be represented internally as $colon$minus$greater.Mixed IdentifiersA mixed identifier consists of an alphanumeric identifier, which is followed by an underscore and an operator identifier.Following are legal mixed identifiers:unary_+, myvar_=Here, unary_+ used as a method name defines a unary + operator and myvar_= used as method name defines an assignment operator (operator overloading).Literal IdentifiersA literal identifier is an arbitrary string enclosed in back ticks (` . . . `).Following are legal literal identifiers:`x` `<clinit>` `yield`Scala KeywordsThe following list shows the reserved words in Scala. These reserved words may not be used as constant or variable or any other identifier names.abstract case catch Class def do else extendsfalse final finally For10forSome if implicit importlazy match new Nullobject override package privateprotected return sealed super this throw trait Trytrue type val Varwhile with yield- : = =><- <: <% >:# @Comments in ScalaScala supports single-line and multi-line comments very similar to Java. Multi-line comments may be nested, but are required to be properly nested. All characters available inside any comment are ignored by Scala compiler.object HelloWorld {/* This is my first java program.* This will print 'Hello World' as the output* This is an example of multi-line comments.*/def main(args: Array[String]) {// Prints Hello World// This is also an example of single line comment.println ("Hello, world!")}}Blank Lines and WhitespaceA line containing only whitespace, possibly with a comment, is known as a blank line, and Scala totally ignores it. Tokens may be separated by whitespace characters and/or comments.11Newline CharactersScala is a line-oriented language where statements may be terminated by semicolons (;) or newlines. A semicolon at the end of a statement is usually optional. You can type one if you want but you don't have to if the statement appears by itself on a single line. On the other hand, a semicolon is required if you write multiple statements on a single line. Below syntax is the usage of multiple statements.val s = "hello"; println(s)Scala PackagesA package is a named module of code. For example, the Lift utility package is net.liftweb.util. The package declaration is the first non-comment line in the source file as follows:package com.liftcode.stuffScala packages can be imported so that they can be referenced in the current compilation scope. The following statement imports the contents of the scala.xml package:import scala.xml._You can import a single class and object, for example, HashMap from the scala.collection.mutable package:import scala.collection.mutable.HashMapYou can import more than one class or object from a single package, for example, TreeMap and TreeSet from the scala.collection.immutable package:import scala.collection.immutable.{TreeMap, TreeSet}Apply DynamicA marker trait that enables dynamic invocations. Instances x of this trait allow method invocations x.meth(args) for arbitrary method names meth and argument lists args as well as field accesses x.field for arbitrary field namesfield. This feature is introduced in Scala-2.10. If a call is not natively supported by x (i.e. if type checking fails), it is rewritten according to the following rules:foo.method("blah") ~~> foo.applyDynamic("method")("blah")12foo.method(x = "blah") ~~> foo.applyDynamicNamed("method")(("x", "blah"))foo.method(x = 1, 2) ~~> foo.applyDynamicNamed("method")(("x", 1), ("", 2))foo.field ~~> foo.selectDynamic("field")foo.varia = 10 ~~> foo.updateDynamic("varia")(10)foo.arr(10) = 13 ~~> foo.selectDynamic("arr").update(10, 13)foo.arr(10) ~~> foo.applyDynamic("arr")(10)13End of ebook previewIf you liked what you saw…Buy it from our store @ https://14。
linux翻译Linux is a free and open-source operating system based on the Unix operating system. Developed by Linus Torvalds in 1991, Linux has become increasingly popular due to its stability, security, and versatility.Linux, as an operating system, is known for its ability to run on a wide range of hardware devices, from smartphones and tablets to servers and mainframes. Its scalability allows it to be used in various industries, including education, finance, and healthcare.One of the key advantages of Linux is its open-source nature. This means that the source code of the operating system is freely available, allowing users to modify and customize it according to their needs. This flexibility has led to the development of countless distributions (or "distros") of Linux, each tailored for specific purposes or user preferences.Linux also has a strong emphasis on security. Its architecture is designed to provide multiple layers of protection against malware and other threats. Additionally, the open-source nature of Linux allows for a large community of developers to continuously monitor and update the system for any vulnerabilities or bugs. Another advantage of Linux is its stability. Due to its UNIX-based design, Linux is less prone to crashes and freezes compared to other operating systems. This reliability makes Linux a popular choice for servers and mission-critical systems.Linux offers a wide range of software applications, both free andproprietary. The package management system in Linux makes it easy to install, update, and manage applications. Popular software such as web browsers, office productivity suites, and multimedia players are available for Linux.In recent years, the popularity of Linux has grown significantly, with many organizations and individuals choosing Linux as their preferred operating system. The ability to avoid licensing costs, along with the freedom to customize the operating system, has attracted businesses and individuals alike.Linux also has a vibrant and supportive community. Numerous online forums, tutorials, and documentation resources are available for users to seek help or share their knowledge. This strong community spirit has contributed to the continued growth and improvement of Linux.In conclusion, Linux is a powerful and versatile operating system that offers stability, security, and customization options. Its open-source nature and wide range of applications make it an attractive choice for both individuals and organizations. With a passionate and supportive community, Linux continues to evolve and thrive in the ever-changing technology landscape.。
使用Shell脚本在Linux环境下实现系统性能分析在Linux服务器管理中,进行系统性能分析是非常重要的一项工作。
通过对系统的性能进行全面评估和分析,可以及时发现并解决各种问题,提高系统的稳定性和性能。
本文将介绍如何使用Shell脚本在Linux环境下实现系统性能分析,帮助管理员更好地监控和调优系统。
一、了解性能指标在进行系统性能分析之前,我们首先需要了解一些基本的性能指标。
以下是一些常用的性能指标:1. CPU使用率:表示CPU在一段时间内的使用情况,一般以百分比表示。
2. 内存使用率:表示内存在一段时间内的使用情况,一般以百分比表示。
3. 磁盘IO速度:表示磁盘读写操作的速度,一般以字节/秒表示。
4. 网络带宽利用率:表示网络带宽在一段时间内的使用情况,一般以百分比表示。
了解这些性能指标可以帮助我们更好地分析和优化系统性能。
二、编写Shell脚本下面我们来编写一个Shell脚本,用于实现系统性能分析。
1. 首先,创建一个名为"performance_analysis.sh"的文件,并添加以下内容:```#!/bin/bash# 获取CPU使用率cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}')# 获取内存使用率mem_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0}')# 获取磁盘IO速度disk_io_speed=$(iostat -d | grep "sda" | awk '{print $2}')# 获取网络带宽利用率network_bandwidth=$(ifstat | grep "ens33" | awk '{print $1}')# 输出性能指标echo "当前CPU使用率:$cpu_usage%"echo "当前内存使用率:$mem_usage%"echo "当前磁盘IO速度:$disk_io_speed bytes/second"echo "当前网络带宽利用率:$network_bandwidth%"```2. 保存并退出文件后,给文件添加执行权限:```chmod +x performance_analysis.sh```三、运行Shell脚本分析系统性能完成上述步骤后,我们就可以运行Shell脚本了,通过执行该脚本可以获取系统的性能指标信息。
linux event处理流程1.事件由硬件或软件引发,传递给内核处理。
The event is triggered by hardware or software and passed to the kernel for processing.2.内核根据事件类型选择相应的处理程序进行处理。
The kernel selects the appropriate handler for processing based on the type of event.3.处理程序可以是系统自带的默认处理程序,也可以是用户自定义的程序。
The handler can be a default system-provided handler or a user-defined program.4.处理程序对事件进行解析和分析,以确定进一步的处理流程。
The handler parses and analyzes the event to determine further processing steps.5.处理程序可能会生成新的事件,将其传递给其他处理程序继续处理。
The handler may generate new events and pass them to other handlers for further processing.6.处理程序执行必要的操作,可能会修改系统状态或触发其他事件。
The handler performs necessary operations, which may modify system state or trigger other events.7.处理程序将处理结果反馈给内核,内核更新系统状态。
The handler provides feedback to the kernel, which updates the system state.8.内核根据处理结果执行相应的动作,完成整个事件处理过程。