Powered By Blogger

miércoles, 8 de mayo de 2013

Use ECMAScript 6 Today

Today, ECMAScript 6 is in the process of being finalized. ECMAScript is the foundation of JavaScript and, hence, exploring the proposed features today also means that we get a sneak peak at how we will be writing JavaScript in the near future! In this article, we’ll explore ten new features, with a significant focus on tools, browsers and transpilers.


A Brief History: ECMA, ECMAScript and JavaScript

JavaScript was originally developed by Brendan Eich of Netscape, and officially released as part of Netscape Navigator 2.0 in 1995. A year later, JavaScript was submitted to ECMA International, a body that facilitates the standardization of information and communication technology and consumer electronics, so that it can be formalized industry-wise. ECMAScript, thus, became the name of the scripting language standardized in ECMA-262.

The ECMAScript standard forms the backbone of many other derived languages, including ActionScript and JScript. Through the years, ECMAScript has gone through four versions, with the discussions today very much revolving around version six, which has also been code-named, ECMAScript Harmony.

Version correspondence

Before we dive into these new features, it’s important to note that the ECMAScript standard forms the foundation of JavaScript. There are numerical differences between each of the JavaScript versions and the corresponding ECMAScript editions. This is to say that JavaScript is compatible with the ECMAScript standard, while providing more features. The table below summarizes the relationship between JavaScript and ECMAScript:

JavaScript Version ECMAScript Edition Year
JavaScript 1.1 ECMAScript edition 1 1997
JavaScript 1.5 ECMAScript edition 3 1999
JavaScript 2.0 ECMAScript Harmony Work in progress

ES6 Overview

Goals

JavaScript has come a long way since its humble beginnings nearly twenty years ago. Today, developers are writing thousands of lines of code creating complex JavaScript applications. Before we dive into the detailed features of ES6, you may want to look at the big picture that is defined in the specification drafts, in terms of requirements, goals, means and themes. One of the goals for ES6 is to be a better language for creating:

  • complex applications
  • libraries
  • code generators

Compatibility

The ES6 compatibility table is very useful, as it tells us the ES6 features that are supported in the current browser. It also gives us a handy link to the specifications for each of the features listed. Do note that some of the features’ existence might not mean full compliance with specifications. When working with Chrome, be sure to enable the “Experimental JavaScript” flags.

Features

Now that the big picture is defined, let’s explore how we can implement them. In the following sections, we will discuss ten features of ES6, using various tools so that we can understand ES6 both in theory and practice. Prior knowledge of JavaScript is a pre-requisite, so feel free to check out many resources on JavaScript.

Listed below are the features that we’ll go through with a different tool. Try them out one by one, or jump to the specific feature that you’d like to explore:

  1. Block scoping with let [ using Firefox browser ]
  2. Block scoping with const [ using Chrome browser ]
  3. Classes [ using Traceur ]
  4. Default function parameters [ using TypeScript ]
  5. Collections [ using NodeJS ]
  6. Destructuring [ using Firefox browser ]
  7. Rest parameters & Spread operator [ using Grunt plugin Traceur ]
  8. Iterators [ using Firefox browser ]
  9. Array comprehension [ using Firefox browser ]
  10. Modules (using ES6 Module Transpiler)

Feature 1 - Block Scoping with let

JavaScript variables are function-scoped. This means that, even if there are variables declared in a nested block, they are available throughout the function. Let’s review a short example below; we’ll simply use the web console in Firefox or Chrome to run them. What do you think will be the value of jsFuture?

  var jsFuture = "es6";  (function () {    if (!jsFuture) { var jsFuture = "es5"; }    console.log(jsFuture);  }());  

In the above example, the value of jsFuture in the console.log statement will be “es5″. Crucial to your understanding is the fact that, in JavaScript, variable declarations are hoisted to the top, but variable initializations, on the other hand, are not. Hence, regardless of where the variables are initialized and declared, within the function scope, they will always be hoisted. The snippet below is exactly the same – with comments to illustrate this feature of variable hoisting.

  var jsFuture = "es6";  (function () {    // var jsFuture = undefined;    // variable hoisting    if (!jsFuture) { var jsFuture = "es5"; }    console.log(jsFuture); // "es5"  }());  

ES6 tackles this issue with let, which is like var, except for the fact that it is block scoped instead of function scoped. Let’s consider another example with var below. Calling the function es[6]() will give us the value of i = 10. Notice that, even though var i = 0; is declared in the for loop, the scope of var i defaults to global. Hence, when the function es[6]() is executed, the value of i is 10.

  var es = [];  for (var i = 0; i < 10; i++) {    es[i] = function () {      console.log("Upcoming edition of ECMAScript is ES" + i);    };  }  es[6](); // Upcoming edition of ECMAScript is ES10  

Let’s now use let. To code this out, we’ll use Firefox and open up the web console through the menu (Tools > Web developer > Web Console). Creating a block-scoped variable within the for loop, let c = i; made it block scoped.

  var es = [];  for (var i = 0; i < 10; i++) {    let c = i;    es[i] = function () {      console.log("Upcoming edition of ECMAScript is ES" + c);    };  }  es[6](); // Upcoming edition of ECMAScript is ES6  

Firefox already supports many upcoming ES6 features. Refer to the compliance table for Firefox to keep updated on which features are supported, and which ones are also compliant with the current specification.


Feature 2 - Block Scoping with const

Constant definitions are now possible with const. let and const behave similarly in the sense that both are block scoped, but with const, the values are read-only and cannot be re-declared later on. Let’s review a simple code example in Chrome:


Feature 3 - Classes

In object-oriented programming languages, a class is a representation of an object. It forms the blueprint, while an object is an instance of a class. With regard to JavaScript, it is a class-less programming language and everything is an object. Traditionally, we’ve used functions and prototypes to implement classes. Let’s explore one common way of implementing class in ES5.

  var Language = function(config) {    this.name = config.name;    this.founder = config.founder;    this.year = config.year;  };    Language.prototype.summary = function() {    return this.name + " was created by " + this.founder + " in " + this.year;  };  

Next, let’s see how ES6 implements classes with minimal class declaration syntax that can be extremely important to distinguish classes and functions. To code out class using the ES6 syntax, we will use Google’s Traceur, which is a transpiler that compiles ES6 code into ES5. First, let’s create the html file structure within which we will insert the ES6 syntax for classes. In order to compile the Traceur code, we require both traceur.js to compile Traceur to JavaScript, as well as bootstrap.js to bind them all. Finally, Traceur will look for script type="text/traceur" tags to compile the relevant code inside the tags into vanilla JavaScript.

  <!DOCTYPE html>  <html>  <head>    <title>ES6 Classes</title>    <script src="https://traceur-compiler.googlecode.com/git/bin/traceur.js"></script>    <script src="https://traceur-compiler.googlecode.com/git/src/bootstrap.js"></script>  </head>  <body>    <script type="text/traceur">      // insert ES6 code    </script>  </body>  </html>  

Next, within the script type="text/traceur" tags, let’s use the ES6 syntax to implement the same class that we previously did for Language.

  class Language {    constructor(name, founder, year) {      this.name = name;      this.founder = founder;      this.year = year;    }    summary() {      return this.name + " was created by " + this.founder + " in " + this.year;    }  }  

We can now create an instance of the class Language by opening the HTML file in the Chrome browser as var js = new Language. In the console, we’ll see the prompts for other properties of the language as well!

With such a clear syntax declaration, we can also move on to extend the class to implement a sub-class MetaLanguage that will inherit all the properties from the parent class Language. Inside the constructor function, we will require the function super that will call the constructor of the parent class so that it is able to inherit all of its properties. Lastly, we can also add on extra properties, such as version, as illustrated in the code below. Let’s review the ES6 syntax and run it in the Chrome browser:

  class MetaLanguage extends Language {    constructor(x, y, z, version) {      super(x, y, z);      this.version = version;    }  }  

Traceur is a useful transpiler that allows us to code using the ES6 syntax, while doing the heavy-lifting for us to compile it back to the current JavaScript version. Do try out other ES6 features in Traceur as well!


Feature 4 - Default Function Parameters

With default function parameters, we can always have function parameters as an option by setting some defaults. The syntax for this feature in ES6 is extremely intuitive. The default parameters are defined when the functions are defined. Let’s have a look at the ES6 syntax below in a new TypeScript file with an extension of *.ts.

  function history(lang = "C", year = 1972) {    return lang + " was created around the year " + year;  }  

Next, we will install TypeScript as an npm module and run the file .*ts and compile it to vanilla JavaScript. Here are the installation and then compilation commands in the command line:

  $ npm install -g typescript  $ npm view typescript version  0.8.3  $ tsc 4-default-params.ts  

The command above will create a vanilla JavaScript file, called 4-default-params.js, which can then be called from an HTML file. Here’s the simple HTML file that will call the external JavaScript file that is created by the TypeScript compiler:

  <!doctype html>  <html lang="en">  <head>    <meta charset="UTF-8">    <title>ES6 Default Parameters</title>  </head>  <body>    <script src="4-default-params.js"></script>  </body>  </html>  

Finally, we will open the HTML file in Chrome/Firefox and call the function history() two times, with and without the function parameters. Notice that not passing in any function parameters will fall back to the default parameters:

Do check out other TypeScript features, including class or go through a TypeScript tutorial for more in-depth usage.


Feature 5 - Collections

ES6 offers new data structures previously not available in JavaScript. Before we jump into exploring two such data structure (Sets and Maps), let’s see how we can run ES6 syntax with NodeJS. Install NodeJS; from here on, we will work in the command line. Firstly, we will check the NodeJS version installed, and then check which options will enable ES6 features with the command node --v8-options | grep harmony.

  $ node --version  v0.10.4    $ node --v8-options | grep harmony  --harmony_typeof (enable harmony semantics for typeof)  --harmony_scoping (enable harmony block scoping)  --harmony_modules (enable harmony modules (implies block scoping))  --harmony_proxies (enable harmony proxies)  --harmony_collections (enable harmony collections (sets, maps, and weak maps))  --harmony (enable all harmony features (except typeof))  

Next, start the NodeJS repl and query which properties are available for Set and Maps. We will start the NodeJS repl with node --harmony to enable all ES6 features.

  $ node --harmony  > Object.getOwnPropertyNames(Set.prototype)  [ 'constructor',    'add',    'has',    'delete' ]  > Object.getOwnPropertyNames(Map.prototype)  [ 'constructor',    'get',    'set',    'has',    'delete' ]  > .exit  $  

Sets

Sets are simple data structures that are similar to arrays, but each value is unique. Let’s create a new file, called 5-sets.js, and insert some code to create, add, delete and query the new set that we will create. Also, note that we will add “Hippo” data twice, but in the set, it will be registered only once!

  var engines = new Set(); // create new Set    engines.add("Gecko"); // add to Set  engines.add("Trident");  engines.add("Webkit");  engines.add("Hippo");  engines.add("Hippo"); // note that Hippo is added twice    console.log("Browser engines include Gecko? " + engines.has("Gecko"));    // true  console.log("Browser engines include Hippo? " + engines.has("Hippo"));    // true  console.log("Browser engines include Indigo? " + engines.has("Indigo"));   // false    engines.delete("Hippo"); // delete item  console.log("Hippo is deleted. Browser engines include Hippo? " + engines.has("Hippo"));    // false  

Run the file in the node repl with the command node --harmony 5-set.js. Note that, even though “Hippo” was added twice to the set, upon deleting it, the set didn’t include it anymore. This once again illustrates that a set is a data structure that can only contain unique values.

Maps

Maps are quite similar to JavaScript object key-value pairs. Using a unique key, we can retrieve the value. In ES6, the key can be any JavaScript data type and not just strings. That’s the interesting part! Let’s create a new file, called 5-map.js, to try out the create, get and delete features:

  var es6 = new Map(); // create new Map    es6.set("edition", 6);        // key is string  es6.set(262, "standard");     // key is number  es6.set(undefined, "nah");    // key is undefined    var hello = function() {console.log("hello");};  es6.set(hello, "Hello ES6!"); // key is function    console.log( "Value of 'edition' exits? " + es6.has("edition") );     // true  console.log( "Value of 'year' exits? " + es6.has("years") );          // false  console.log( "Value of 262 exits? " + es6.has(262) );                 // true  console.log( "Value of undefined exits? " + es6.has(undefined) );     // true  console.log( "Value of hello() exits? " + es6.has(hello) );           // true    es6.delete(undefined); // delete map  console.log( "Value of undefined exits? " + es6.has(undefined) );      // false    console.log( es6.get(hello) ); // Hello ES6!  console.log( "Work is in progress for ES" + es6.get("edition") ); // Work is in progress for ES6  

As shown with the ES6 collections features, NodeJS harmony option already supports others ES6 features such as block scoping, proxies and modules. Do try them out in NodeJS as well!


Feature 6 - Destructuring

In programming languages, the term “destructuring” denotes pattern matching. In ES6, we can do some pretty nifty pattern matching in arrays and objects that previously would have taken us more than one step. Let’s explore some of them by coding it out in Firefox web console.

Array destructuring

With array destructing, we can initialize variables at once, or even swap them instead of having the conventional way of creating a var temp; temporary variable.

  var [ start, end ] = ["earth", "moon"] // initialize  console.log(start + " calling " + end); // earth calling moon    [start, end] = [end, start] // variable swapping  console.log(start + " calling " + end); // moon calling earth  

Destructuring also becomes a useful shorthand when returning multiple values from a function, as we do not need to wrap around an object anymore. Also, to skip certain variables, just leave the array element empty:

  function equinox() {    return [20, "March", 2013, 11, 02];  }  var [date, month, , ,] = equinox();  console.log("This year's equinox was on " + date + month); // This year's equinox was on 20March  

Object destructuring

Due to destructuring, variables can also be initialized from an object that is returned from a function even with deeply nested objects. Also, just like the array patterns, we can skip the ones not needed. Here’s the snippet of code that illustrates just this:

  function equinox2() {    return {      date: 20,      month: "March",      year: 2013,      time: {        hour: 11, // nested        minute: 2      }    };  }    var { date: d, month: m, time : { hour: h} } = equinox2();  // h has the value of the nested property while "year" and "minute" are skipped totally    console.log("This year's equinox was on " + d + m + " at " + h); // This year's equinox was on 20March at 11  

Feature 7 - Rest Parameters and Spread Operators

Rest parameters

In ES6, rest parameters allows us to easily use a few fixed parameters in a function, along with the rest of the trailing and variable number of parameters. We already use arguments, which is an array-like object that defines the arguments passed to a function, but clearly we cannot use the array function to manipulate these arguments. With a clear syntax in ES6, it also moves the intent of the developer into the syntax level with three dots ... to denote a variable number of arguments.

Let’s try to use rest parameters in the ES6 syntax with gruntjs and its plugin for the traceur transpiler, which we used in the previous section.

  1. Install grunt command line utility:

          $ npm uninstall -g grunt      $ npm install -g grunt-cli        
  2. Create a file, called package.json, which will define the various modules needed to run Grunt. Note that this list of dependencies includes the traceur plugin:

          {        "name": "rest-params",        "version": "0.1.0",        "devDependencies": {          "grunt": "0.4.1",          "grunt-traceur": "0.0.1"        }      }        
  3. Create the Gruntfile.js which will contain just one task traceur that will convert ES6 syntax to today’s JavaScript. With this, we will be able to try out ES6 rest parameters.

          module.exports = function(grunt) {          grunt.initConfig({          pkg: grunt.file.readJSON('package.json'),          traceur: {            custom: {              files:{              'js/': ['rest-spread.js']  // dest : 1              }            }          }        });          grunt.loadNpmTasks('grunt-traceur');        grunt.registerTask('default', ['traceur']);        };          
  4. Create a simple index.html to call the traceur-compiled JavaScript file, js/rest-spread.js:

          <!DOCTYPE html>      <html>      <head>        <title>ES6 Rest parameters</title>      </head>      <body>        <script src="js/rest-spread.js"></script>      </body>      </html>        
  5. Most importantly, we’ll create the file rest-spread.js, which will contain the rest parameter syntax:

          function push(array, ...items) { // defining rest parameters with 3 dot syntax        items.forEach(function(item) {          array.push(item);          console.log( item );        });      }        // 1 fixed + 4 variable parameters      var planets = [];      console.log("Inner planets of our Solar system are: " );      push(planets, "Mercury", "Venus", "Earth", "Mars"); // rest parameters        
  6. Finally, we will run grunt in the command line, which will, by default, run the traceur task and create the file, js/5-rest-spread.js. Next, just view the file index.html in the browser console:

          $ npm install      $ grunt      ╰─$ grunt      Running "traceur:custom" (traceur) task      js/ [ 'rest-spread.js' ]      Compiling... js/      Compilation successful - js/      Writing... js/      js/rest-spread.js successful.      Writing successful - [object Object]        

Spread operator

A spread operator is the opposite of rest parameters. When calling a function, we can pass in the fixed argument that is needed along with an array of a variable size with the familiar three dot syntax, to indicate the variable number of arguments.

We will use the same project as the rest parameters above and append in the spread operator code to the file rest-spread.js. In the example below, the function requires six separate arguments. When calling the function, the data is passed as an array with the spread operator. Let’s see how the syntax looks, when calling the function with fixed arguments as well as a variable number of arguments:

  1. Append the spread operator code to rest-spread.js:

          // Spread operator "...weblink"      function createURL (comment, path, protocol, subdomain, domain, tld) {            var shoutout = comment              + ": "              + protocol              + "://"              + subdomain              + "."              + domain              + "."              + tld              + "/"              + path;          console.log( shoutout );      }        var weblink = ["hypertext/WWW/TheProject.html", "http", "info", "cern", "ch"],        comment = "World's first Website";        createURL(comment, ...weblink ); // spread operator          
  2. Run the traceur compile through the Grunt task in the command line, and view the file, index.html, in the browser:

          $ grunt      Running "traceur:custom" (traceur) task      js/ [ 'rest-spread.js' ]      Compiling... js/      Compilation successful - js/      Writing... js/      js/rest-spread.js successful.      Writing successful - [object Object]        Done, without errors.        

If you’re already using GruntJS as a build tool in your current project, it will be easy to integrate it with ES6 plugins. So do try out other GruntJS ES6-related plugins to compile ES6 syntax to current JavaScript.


Feature 8 - Iterators

JavaScript offers for-in for iteration, but it has some limitations. For example, in an array iteration, the results with a for-in loop will give us the indexes and not the values. Let’s take a look at the code below to illustrate this:

  var planets = ["Mercury", "Venus", "Earth", "Mars"];  for (p in planets) {    console.log(p); // 0,1,2,3  }    var es6 = {    edition: 6,    committee: "TC39",    standard: "ECMA-262"  };  for (e in es6) {    console.log(e); // edition, committee, standard  }  

Let’s try the same concept, but, this time, with for-of with an array, a set and a map:

  var planets = ["Mercury", "Venus", "Earth", "Mars"];  for (p of planets) {    console.log(p); // Mercury, Venus, Earth, Mars  }    var engines = Set(["Gecko", "Trident", "Webkit", "Webkit"]);  for (var e of engines) {      console.log(e);      // Set only has unique values, hence Webkit shows only once  }    var es6 = new Map();  es6.set("edition", 6);  es6.set("committee", "TC39");  es6.set("standard", "ECMA-262");  for (var [name, value] of es6) {    console.log(name + ": " + value);  }  

Feature 9 - Array Comprehension

Array comprehensions give us a shorthand syntax to manipulate each of the array contents in a certain pattern. It is very similar to the map() or filter() methods available in the Array object. Let’s examine how we are using map()

  var temperature = [0, 37, 100];  function degToKelvin(deg) {    return deg + 273;  }  temperature.map(degToKelvin); // [273, 310, 373]  

Let's run through the same feature in Firefox to see the shorthand syntax in ES6 to create arrays with as many as three loops to create possible solutions for the game, Cluedo:

  // Array created with 1 loop  var temperature = [0, 37, 100];  [t + 273 for (t of temperature)]; // [273, 310, 373]    // Array created with 3 loops  var suspects = ["Miss Scarlet", "Colonel Mustard"],    weapons = ["Candlestick", "Dagger"],    rooms = ["Kitchen", "Ballroom"];    [(console.log(s + " with a " + w + " in the " + r)) for (s of suspects) for (w of weapons) for (r of rooms)];  

Feature 10 - Modules

In programming languages, modules perform isolated discrete functions and are independent of one another. This helps to not only build reusable components across projects, but also keeps errors isolated to the parts related to the current project. We have been creating modules in JavaScript typically with AMD or CommonJS. Let's create a simple module using the ES6 syntax and ES6 Module transpiler.

  1. First, let's create the HTML file, index.html, that will call the essential JavaScripts. We'll be using RequireJS as an AMD loader; hence, we refer to a CDN copy of the latest RequireJS file. Next, we also add the attribute, data-main, on the script tag to tell RequireJS to load the js/init.js file.

          <!DOCTYPE html>      <!doctype html>      <html lang="en">      <head>        <meta charset="UTF-8">        <title>ES6 Modules</title>      </head>      <body>        <script src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.5/require.min.js" data-main="js/init"></script>      </body>      </html>        
  2. Now, we will create the file, js/init.js, which will just invoke the js/main.js file:

          require(['main'],        function(){        });        
  3. Create the module, circle, in the file, in/circle.js, in the ES6 syntax. This module exports two functions:

          export function area(radius) {        return Math.PI * radius * radius;      }        export function circumference(radius) {        return 2 * Math.PI * radius;      }        
  4. Create the file, in/main.js, that will import the module circle so that we can use the functions of that particular module. Notice the import syntax:

          import { area, circumference } from 'circle';        console.log("Area of the circle: " + area(4) + " meter squared");      console.log("Circumference of the circle: " + circumference(14) + " meters");        
  5. At this point, the folder structure is shown below. We'll use the ES6 Module transpiler to create ES5 compatible code with two newly created files: js/circle.js and js/main.js.

          $ tree      .      |-- in      |   |-- circle.js      |   `-- main.js      |-- index.html      `-- js          `-- init.js        
  6. Install the ES6 Module Transpiler:

          $ npm install https://github.com/square/es6-module-transpiler.git      $ compile-modules --help        
  7. Finally, we will transpile these two files. Navigate to the folder, in, from the command line:

          $ compile-modules circle.js --type amd --to ../js      $ compile-modules main.js --type amd --to ../js      $ cd ..      $ tree      .      |-- in      |   |-- circle.js      |   `-- main.js      |-- index.html      `-- js          |-- circle.js          |-- init.js          `-- main.js        
  8. Do look at the transpiled code in the files js/circle.js and js/main.js. We will now open up the file, index.html, in the browser to see modules in action! We will need to use a web server to run this file. I'm using the Python SimpleHTTPServer. Navigate to the command line in the root of the file, index.html:

          $ python -m SimpleHTTPServer 8000        

Resources

Many of our web development community members have openly shared about ES6 and what's coming up. I highly recommend going through their blog categories related to ES6:

And, for some further reading:


Play with ES6 Today

There you have it: ten features of ES6 with tools that allow us to code with the new syntax today. I hope this has made you more excited about what's to come! Please note that, since the standardization work is in progress, the syntax, features and compliances might change. Nonetheless, it's definitely worth the effort to dig in sooner than later.



15 Command Line Tools for Monitoring Linux Systems

Do you need to monitor your Linux server’s performance? Most Linux distributions come equipped with many built-in monitoring tools. These tools allow you to retrieve information about system activities, and can be used to find possible causes for your server’s performance issues.

The commands discussed in this article are some of the most basic commands when it comes to system analysis and debugging server issues, such as discovering disk, CPU, memory and network bottlenecks.


1 - top – The Process Activity Command

The top command provides a dynamic, real-time view of the running system (i.e. actual process activity). By default, it displays the most CPU-intensive tasks running on the server and updates the list every five seconds.

Commonly Used Hot Keys

Most Linux distributions come equipped with many built-in monitoring tools.

There are several useful hot keys used with the top command:

  • t — toggles summary information off and on.
  • m — toggles memory information off and on.
  • A — sorts the display by top consumers of various system resources. This is useful for quickly identifying performance-hungry tasks.
  • f — enters an interactive configuration screen for top. It’s helpful for configuring top for a specific task.
  • o — enables you to interactively select the order of the displayed fields.
  • r — issues the renice command.
  • k — issues the kill command.
  • z — toggles between color and monochrome.

2 - vmstat – System Activity, Hardware and System Information

The vmstat command reports virtual memory statistics: processes, memory, paging, block IO, traps and cpu activity.

Syntax:

  # vmstat 3  

Sample output:

      procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------      r b swpd free buff cache si so bi bo in cs us sy id wa st      0 0 0 2540988 522188 5130400 0 0 2 32 4 2 4 1 96 0 0      1 0 0 2540988 522188 5130400 0 0 0 720 1199 665 1 0 99 0 0      0 0 0 2540956 522188 5130400 0 0 0 0 1151 1569 4 1 95 0 0      0 0 0 2540956 522188 5130500 0 0 0 6 1117 439 1 0 99 0 0      0 0 0 2540940 522188 5130512 0 0 0 536 1189 932 1 0 98 0 0      0 0 0 2538444 522188 5130588 0 0 0 0 1187 1417 4 1 96 0 0      0 0 0 2490060 522188 5130640 0 0 0 18 1253 1123 5 1 94 0 0   

Display memory utilization slabinfo with the following command:

  # vmstat -m  

And you can retrieve information about active and inactive memory pages with:

  # vmstat -a  

3 - w – Logged In Users

The w command displays information about the currently logged in users and their processes. Its syntax is:

      # w [user]  

Running this command gives you output similar to the following:

      17:58:47 up 5 days, 20:28, 2 users, load average: 0.36, 0.26, 0.24      USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT      root pts/0 10.1.3.145 14:55 5.00s 0.04s 0.02s vim /etc/resolv.conf      root pts/1 10.1.3.145 17:43 0.00s 0.03s 0.00s w  

4 - uptime – System Uptime

The uptime command not only displays the amount of time the server has been running, but the current time, how many users are currently logged on and the system load average for the past 1, 5, and 15 minutes.

  # uptime  

Its output will look similar to:

  11:57:40 up 96 days, 17:23,  0 users,  load average: 1.17, 1.15, 1.14  

5 - ps – The Processes

The ps command reports a snapshot of the current processes. To select all processes, use the -A or -e option.

  # ps -A  

Running this command on your system will provide similar results to the following:

       PID TTY          TIME CMD      6538 ?        00:00:00 anytermd      6543 pts/4    00:00:00 bash      6855 pts/4    00:00:00 ps  

To show long format output, add “l” at the end:

  # ps -Al  

Turn on extra-full mode to show the command line arguments passed to the processes by adding “F”:

  # ps -AlF  

Add an “H” to see threads (LWP and NLWP):

  # ps -AlFH  

To see threads after processes, add “m”:

  # ps -AlLm  

Or print a process tree:

      # ps -ejH      # ps axjf      # pstree  

Display only the process IDs of lighttpd:

  # ps -C lighttpd -o pid=  

Or:

  # pgrep lighttpd  

You can also find the top ten processes using the most CPU with:

  # ps -auxf | sort -nr -k 3 | head -10  

6 - free – Memory Usage

The free command displays the total amount of physical and swap memory in the system, as well as the buffers used by the kernel.

  # free  

This simple command’s output looks like:

      total used free shared buffers cached      Mem: 12302896 9739664 2563232 0 523124 5154740      -/+ buffers/cache: 4061800 8241096      Swap: 1052248 0 1052248  

7 - iostat – Average CPU Load and Disk Activity

The iostat command reports CPU and input/output statistics for devices, partitions and network filesystems (NFS).

  # iostat  

And the obligatory sample output:

      Linux 3.4.5-hardened-v3 (tryit)         12/14/12        _i686_  (1 CPU)         avg-cpu:  %user   %nice  %system %iowait  %steal  %idle                 2.47    6.52    2.54    1.57    0.00   86.90             Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn  

8 - sar – Collect and Report System Activity

The sar command collects, reports and saves system activity information. To see the network counter, enter:

  # sar -n DEV | more  

To display the network counters from the 24th:

  # sar -n DEV -f /var/log/sa/sa24 | more  

You can also display real time usage using sar:

  # sar 4 5  

Sample output:

      Linux 3.4.5-hardened-v3 (tryit)         12/14/12        _i686_  (1 CPU)         12:45:25        CPU     %user     %nice   %system   %iowait    %steal     %idle      12:45:29        all      0.43     19.70      1.07      0.00      0.00     78.80      12:45:33        all      0.43     19.35      1.51      1.51      0.00     77.20      12:45:37        all      0.43     18.49      1.94      0.00      0.00     79.14      12:45:41        all      0.43     19.02      2.14      0.00      0.00     78.42      12:45:45        all      0.65     18.49      2.37      0.00      0.00     78.49      Average:        all      0.47     19.01      1.80      0.30      0.00     78.41  

9 - mpstat – Multi-processor Usage

The mpstat command displays each available processor’s activities, with processor 0 being the first. Use the following command to display the average CPU utilization per processor:

  # mpstat -P ALL  

And this is the output:

      Linux 3.4.5-hardened-v3 (tryit)         12/14/12        _i686_  (1 CPU)         12:47:46     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest    %idle      12:47:46     all    2.47    6.52    1.48    1.57    0.00    1.06    0.00    0.00    86.90      12:47:46       0    2.47    6.52    1.48    1.57    0.00    1.06    0.00    0.00    86.90  

10 - pmap – Process Memory Usage

The pmap command reports the memory map of a process. Use this command to find the causes of memory bottlenecks.

  # pmap -d PID  

To display process memory information for pid # 47394, enter:

  # pmap -d 47394  

Sample output:

      47394: /usr/bin/php-cgi      Address Kbytes Mode Offset Device Mapping      0000000000400000 2584 r-x-- 0000000000000000 008:00002 php-cgi      0000000000886000 140 rw--- 0000000000286000 008:00002 php-cgi      00000000008a9000 52 rw--- 00000000008a9000 000:00000 [ anon ]      0000000000aa8000 76 rw--- 00000000002a8000 008:00002 php-cgi      000000000f678000 1980 rw--- 000000000f678000 000:00000 [ anon ]      000000314a600000 112 r-x-- 0000000000000000 008:00002 ld-2.5.so      000000314a81b000 4 r---- 000000000001b000 008:00002 ld-2.5.so      000000314a81c000 4 rw--- 000000000001c000 008:00002 ld-2.5.so      000000314aa00000 1328 r-x-- 0000000000000000 008:00002 libc-2.5.so      000000314ab4c000 2048 ----- 000000000014c000 008:00002 libc-2.5.so      .....      ......      ..      00002af8d48fd000 4 rw--- 0000000000006000 008:00002 xsl.so      00002af8d490c000 40 r-x-- 0000000000000000 008:00002 libnss_files-2.5.so      00002af8d4916000 2044 ----- 000000000000a000 008:00002 libnss_files-2.5.so      00002af8d4b15000 4 r---- 0000000000009000 008:00002 libnss_files-2.5.so      00002af8d4b16000 4 rw--- 000000000000a000 008:00002 libnss_files-2.5.so      00002af8d4b17000 768000 rw-s- 0000000000000000 000:00009 zero (deleted)      00007fffc95fe000 84 rw--- 00007ffffffea000 000:00000 [ stack ]      ffffffffff600000 8192 ----- 0000000000000000 000:00000 [ anon ]      mapped: 933712K writeable/private: 4304K shared: 768000K  

The last line is very important:

  mapped: 933712K total amount of memory mapped to files  writeable/private: 4304K the amount of private address space  shared: 768000K the amount of address space this process is sharing with others  

11 - netstat – Network Statistics

The netstat command displays both incoming and outgoing network connections, routing tables and a number of network interface statistics. It is available on Unix, Unix-like systems, and Windows NT-based operating systems.

   # netstat   

12 - iptraf – Real-time Network Statistics

The iptraf command is an colorful, interactive IP LAN monitor. It is an ncurses-based IP LAN monitor that generates various network statistics including TCP info, UDP counts, ICMP and OSPF information, Ethernet load info, node stats, IP checksum errors and more. It can provide the following info in easy to read format:

  • Network traffic statistics by TCP connection.
  • IP traffic statistics by network interface.
  • Network traffic statistics by protocol.
  • Network traffic statistics by TCP/UDP port and packet size.
  • Network traffic statistics by Layer2 address.

13 - tcpdump – Detailed Network Traffic Analysis

The tcpdump is a simple command that dumps a network’s traffic. You need, however, a good understanding of the TCP/IP protocol in order to use this tool. For example, to display traffic info about DNS, enter:

   # tcpdump -i eth1 'udp port 53'  

To display all IPv4 HTTP packets from port 80 (i.e. print only packets that contain data; not, for example, SYN abd FIN packets and ACK-only packets), enter:

   # tcpdump 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'  

To display all HTTP sessions to 192.168.1.5:

  # tcpdump -ni eth0 'dst 192.168.1.5 and tcp and port http'  

14 - strace – System Calls

The strace command traces system calls and signals. This is useful for debugging the webserver and other server problems.

The following command runs strace against /bin/foo and captures its output to output.txt:

   # strace -o output.txt /bin/foo  

15 - /proc – Various Kernel Statistics

The /proc file system provides detailed information about various hardware devices and other Linux kernel information. Common /proc examples are:

      # cat /proc/cpuinfo      # cat /proc/meminfo      # cat /proc/zoneinfo      # cat /proc/mounts   

Conclusion

And there you have it: fifteen useful commands that let you monitor different aspects of your Linux system. Naturally, these commands are only a small subset of the many that Linux provides. But for day to day operations, they are usually enough.

Have a favorite command? Let’s keep the conversation going within the comments area.



domingo, 5 de mayo de 2013

Sudoku alfabético 25

Sudoku25

Es como un sudoku normal y corriente pero con 25 «números» que son letras, de la A a la Y.

Lo encontré en The Golden Ticket: P, NP, and the Search for the Impossible de Lance Fortnow, un altamente recomendable libro sobre teoría de la complejidad computacional que examina la cuestión P=NP en plan divulgativo, y donde se tocan problemas matemáticos y lógicos bien conocidos, entre ellos la complejidad de los sudokus.

# Enlace Permanente y Comentarios



10 millones de dólares para construir el museo de Nikola Tesla

Wikipedia

Admiradores del inventor de origen serbio planean construir un museo en homenaje a Tesla en la famosa torre Wardenclyffe, su última gran obra

Si hablamos de inventores prolíficos, además de ingenieros visionarios, el nombre de Nikola Tesla ha de salir irremediablemente en nuestra conversación. Nacido en 1856, Tesla fue el responsable de que la electricidad llegara de manera fácil desde las centrales hasta nuestras casas.

No contento con eso, el ingeniero de origen serbio también inventó el motor eléctrico, contaba con una patente sobre la radio y fui pionero en tratar de mover cosas mediante control remoto. Entre las aportaciones poco conocidas de Nikola Tesla también se encuentran la lámpara fluorescente y el submarino eléctrico.

Tras trabajar en París, aceptó una oferta para desplazarse a Estados Unidos, donde realizaría sus mayores contribuciones como inventor. Allí en su laboratorio situado en la famosa torre Wardenclyffe, Nikola Tesla continuó con su imaginación y trabajo, con propuestas e invenciones increíbles, pero también con excentricidades, que en parte explican por qué hoy no es un científico demasiado reconocido, a la altura de otros como Edison o Einstein.

Hoy en día los papeles de trabajo de este ingeniero ya son propiedad de su familia, aunque durante mucho tiempo fueron custodiados de forma secreta por el Gobierno de Estados Unidos. Su trabajo es difundido en en Museo Nikola Tesla, situado en Belgrado, donde se guardan buena parte de sus publicaciones, archivo personal o correspondencia.

Sin embargo, desde hace tiempo existe una iniciativa estadounidense para recuperar la torre Wardenclyffe, la última gran idea de Nikola Tesla, pues planeaba construir allí un pionero punto de telecomunicaciones, con el objetivo de usarla para la telefonía comercial transatlántica, la retransmisión por radio y en último caso, poder demostrar desde esta torre la transmisión de energía sin cables conectores. Finalmente, el proyecto no pudo ser terminado debido a problemas financieros.

Este espacio se puso a la venta por 1,6 millones de dólares, y estuvo a punto de ser comprada con el objetivo de construir allí un centro comercial. Por suerte, Matthew Inman promovió la recaudación de 850.000 doláres para salvaguardar este histórico edificio.

La venta de aquel emplazamiento es hoy una realidad y los admiradores de Nikola Tesla planean recaudar a partir de ahora 10 millones de dólares, con el objetivo de construir allí un museo en homenaje al inventor de lo que conocemos como electricidad comercial. Aunque ya existe uno situado en Serbia, lo cierto es que convertir la torre Wardenclyffe en un edificio en memoria a Nikola Tesla está cada vez más cerca de convertirse en una realidad.





¿Cómo funcionan las velas solares?

Vela Solar IKAROS(Wikimedia Commons)

Los curiosos de la astronomía suelen preguntarse cómo funcionan las velas solares, y aunque el sistema lleve en su nombre la palabra "vela", el viento no influye en lo absoluto.

Las velas solares son un sistema que está poco a poco tomando mayor popularidad en la comunidad científica del transporte espacial. Al presentar una opción natural para el desplazamiento de una sonda o satélite, que ofrece el ahorro del gasto en combustibles, se ha convertido en uno de los principales campos de investigación de la NASA, y por ello ya existen algunas naves espaciales que cuentan con este sistema. Sin embargo, muchos se preguntan, ¿cómo funcionan las velas solares?

Hace algunos días tratábamos aquí, en ALT1040, un tema relacionado con el problema de la basura espacial, el cual podría ser solucionado mediante el uso de un rayo láser. Aunque la solución no era la definitiva, si consiste en dar más seguridad a los satélites y la Estación Espacial Internacional que transitan por la órbita de nuestro planeta. Si bien se piensa que los láser sirven solamente para calentar objetos a corta distancia, al aplicarse una cantidad de energía suficiente y gracias a la falta de gravedad en el espacio, se pueden mover objetos como la basura espacial, gracias a que representa un flujo de luz y sus partículas en lugar de calentar el objeto, lo mueve. Lentamente, pero lo mueve, según las pruebas realizadas por los científicos.

Este, es el mismo caso que presenta el sistema de velas solares. A diferencia de lo que muchos podrían creer, que las sondas con velas solares se mueven bajo el efecto del llamado "viento solar" (que sí es el responsable de las auroras), no es así. Las velas solares son impulsadas gracias a presión que ejerce sobre ellas la radiación solar, o en otras palabras, la luz del Sol.

Es un concepto un poco confuso dado que se entiende que la luz no tiene masa, y por ende no debería poder "empujar" nada. Aún así, la luz solar si posee algo que la mecánica llama "cantidad de movimiento", y cuando las velas solares reciben las partículas o fotones de la luz, esta cantidad de movimiento se traspasa a la nave espacial, y por ello se mueve. Así de sencillo.

Por supuesto, esto requiere que dichas velas estén fabricadas de materiales específicos. En el caso de la nave Cosmo 1 de la NASA o la sonda IKAROS de Japón, la primera lanzada en el año 2005 y la segunda en el año 2010, sus velas están fabricadas de un material especial llamado Kapton que permite atrapar los fotones, incluso cuando tiene un grosor de apenas 5 micras (0.005 milímetros).

Para el año 2014 se espera que se lance la vela solar más grande del mundo, con el nombre de Sunjammer, en honor al autor Arthur C. Clarke, quien fue el primero en tratar el concepto de este sistema en sus libros de ciencia ficción hace casi 50 años.