Setting up a Sun Enterprise 250 (E250)

Recently I needed to install Solaris 10 on an old creaky Sun Enterprise 250 (E250) and 450 (E450) box. This quick how-to documents what I did to get the machine up and running, and answers a few of the questions it took me a while to figure out along the way! Most of it is pretty applicable to a modern Sun too.

Getting to ok

The first thing you have to do is get to the OK prompt. On most boxes I have used in the past, getting to ok is easy, just power on the machine and it stops at the ok prompt, waiting for someone to type boot, however this machine was different – it just booted straight away, without an ok in sight!

To get the ok prompt, wait for the system to initialise the memory and devices and either press STOP-A (if you are using a VVT220, it will probably have a STOP key) or ctrl-break repeatedly.

Now that you have got the OK prompt, you probably want to stop the machine auto-booting until you have it working the way you want it. To stop auto-booting, enter the following commands at the ok prompt:


ok setenv auto-boot-on-error? false
ok setenv auto-boot? false

Now power off the machine and let it restart (apparently you need to do this, as STOP-a or ctrl-break stops the machine initialising some things, so its best to restart to be sure you don’t hit problems later).

Installing Solaris 10

Installing Solaris 10 is pretty simple at this point – pop the DVD in the drive and enter:


ok boot cdrom

The installer will kick off, asking you a bunch of questions along the way – make sure and have your hostname, domain, network and DNS settings to hand.

Before the OS actually installs, you need to layout your disk. How you set this up is totally up to you – I usually create a parition for each of /, /usr, /export/home, /opt, /tmp and /var/crash on the primary disk. Sizes will very much depend on your setup and requirements.

Getting Rid of the Desktop and Login Server

By default, Solaris 10 attempts to run the desktop environment on boot. If, like me, you want to use this machine as a server and have no need for a desktop, then save yourself some RAM and disable it:


# svcadm disable svc:/application/graphical-login/cde-login:default
# /usr/dt/bin/dtconfig -d

(Thanks to this site for that tip!)

ZFS Rocks

I knew in the back of my mind that Solaris 10 had a fancy new filesystem called ZFS, but I didn’t know what that meant until I came across this article. If you have a sever with more than one disk, or you are not sure what sizes each of your mountpoints should be, or you need striping, mirroring or RAID, then go read up on ZFS. It just might make disk management as pleasurable as writing Ruby!

October 8, 2007 at 9:22 pm Leave a comment

ActiveRecord, Oracle and stored procedures

After much painful searching for a built in way to get ActiveRecord to execute a stored procedure on Oracle complete with in and out bind variables I have concluded that its just not possible.

The Oracle connection adapter uses OCI8 under the covers to connect to Oracle and many of its methods are exposed to you. To execute a stored procedure with OCI 8, one method is like this:


require 'oci8'
conn = OCI8.new(user, password, db.world)
res = conn.exec('begin my_proc(:in, :out); end;', 'invalue', 'outvalue')

That’s pretty simple, I guess. To do such magical things with ActiveRecord, a method ‘execute’ is exposed, this looks like this:


def execute(sql, name = nil) #:nodoc:
  log(sql, name) { @connection.exec sql }
end

The method spec for OCI8#exec is however


def exec(sql, *args, &blk)
  ...
end

So ActiveRecord gives us a wrapper to exec, but sadly removes the essential bind variables.

In my application I added the following code:


class ActiveRecord::ConnectionAdapters::OracleAdapter
  def _exec(stm, *args)
    @connection.exec(stm, *args)
  end

  def _cursor(stm)
    @connection.parse(stm)
  end
end

Which lets me execute Oracle stored procedures using my ActiveRecord connection at last!

My application is not a Rails application as such, more an application that uses ActiveRecord, so I put the above code in my boot script after including ActiveRecord – not sure exactly where you would put it in a Rails app.

If there is a better built in way to do this, please comment and let me know as I have not been able to find it!

UPDATE – it seems there are a few other struggling to get this stuff to work – I have added a chunk of code below that makes this stuff work:


require "rubygems"
require_gem "activerecord"

ActiveRecord::Base.establish_connection(:adapter => "oracle",
                                        :database => "avdev7",
                                        :username => "sodonnel2",
                                        :password => "sodonnel2")

class ActiveRecord::ConnectionAdapters::OracleAdapter
  def _exec(stm, *args)
    @connection.exec(stm, *args)
  end

  def _cursor(stm)
    @connection.parse(stm)
  end
end

# create or replace procedure testproc(inxml in varchar2, outxml out varchar2)
# is
# begin
#     outxml := inxml;
# end;
# /

class PLSQL < ActiveRecord::Base
  attr_accessor :outxml
  def initialize
    execute
  end

  def execute
    cursor = connection._cursor("BEGIN testproc(:inxml, :outxml);  end;");
    xmltext = 'some text to print'
    cursor.bind_param("inxml", xmltext)
    cursor.bind_param("outxml", nil, String, 1000)
    cursor.exec
    @outxml = cursor['outxml']
    cursor.close
  end
end

obj = PLSQL.new
puts "The returned value is : #{obj.outxml}"

August 29, 2007 at 12:07 pm 4 comments

Of course it’s the Database, I don’t need a Profiler

We all know that when any database backed application starts to behave slowly, its always the databases fault, right? After all, if the application code hasn’t change so what else could it be?

If you have spent much time around Oracle, you are sure to have seen complex queries when Oracle decides to changes its query plan for “no good reason” and application performance changes drastically. Perhaps this is what leads to all this finger pointing at the database, mostly by people who don’t understand how it works.

Last week I was contacted to investigate a performance problem in an application that had everyone baffled. The system works off 15 – 20 queues of requests, and a batch job is responsible for processing each queue, so there are up to 20 batch jobs running at once. Normal throughput for a batch job is several requests per second, but this had dropped to several a minute and things were backing up.

Lost Time

The DBAs had provided me with a level 1 trace file captured during this slowdown that looked very normal – no bad queries in sight. Only strange thing was a lot of walk clock time in the trace compared to CPU seconds (5 or 6 times more elapsed time). My initial reaction was certain there was a locking issue somewhere in the database, so I asked the DBAs to create a level 8 trace file that would surely should show me where the all this “lost time” had gone.

No such luck, Oracle was not waiting on anything, so where had all this time gone?

Perhaps the database server was under heavy load you say? Not so, this was a 24 core server 50% idle through the test, humm.

Luckily, I had read Optimising Oracle Performance and actually understand much of the gibberish inside an SQL Trace file. Still mystified where all the time had gone, I fired up Emacs and used Ruby to tell me the biggest time between two database calls, which stood at an impressive 30 seconds between a fetch on cursor 18 and exec on 19 – ie the “lost time” was in the application somewhere between those two queries, not the database calls.

In this case its a bit greyer, as the application is written in PLSQL, but the problem turned out to be the OS struggling to open log files in a directory containing over 2 million files!

Power to the Profiler

The morale of this story, apart from the need to housekeep your log files, is that its important to have the ability to profile your application code as well as the database calls.

Instead of writing scripts to parse Oracle trace files, I should have ran the code through DBMS_Profiler which would have solved this mystery in about two minutes and saved much head scratching and other people making ridiculous suggestions to change irrelevant database parameters!

July 28, 2007 at 6:26 pm Leave a comment

Emacs and Oracle

After learning so much about Emacs to get my Rails setup working the way I wanted it, I though it was about time I figured out how to use SQLPLUS in Emacs too. Turns out it was really easy, as sql-mode is built right in, no .emacs changes required or extra files to download.

To connect to SQLPLUS in an emacs buffer, fire up emacs and type ALT-x sql-oracle. Emacs will prompt you for a username and password and a database to connect to. The database name will need to be in your local tnsnames.ora – in other words, if you cannot sqlplus username/password@database, emacs will not be able to connect either.

You can now enter commands into SQLPLUS just like normal, only inside Emacs. The thing that always frustrated me about SQLPLUS is that there is no command history recall, so I always found myself writing a query in an editor, and copying and pasting into SQLPLUS. Not anymore … thanks to Emacs.

When you enter sql-oracle mode and login, Emacs splits your window in two. You can then edit your SQL in the original window and send the query to SQLPLUS in the other buffer in (at least) one of two ways:

  • Send the entire buffer by typing ALT-x sql-send-buffer
  • Select a region to send by typing ALT-x sql-send-region, or if you buffer is in sql-mode, type CTRL-C CTRL-r

You can of course bind a key sequence to each of these commands to save on the typing if you use them a lot!

If you have an SQLPLUS buffer open for a while, it could get very large. To truncate it, use the command ALT-x comint-truncate-buffer, or add the following to your .emacs:


(add-hook 'sql-interactive-mode-hook
              (function (lambda ()
                          (setq comint-output-filter-functions 'comint-truncate-buffer
                                comint-buffer-maximum-size 5000
                                comint-scroll-show-maximum-output t
                                comint-input-ring-size 500))))

Which will keep your buffer under 5000 lines.

July 10, 2007 at 12:54 pm 4 comments

Emacs and Rails

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

In the last article, I described how to get Ruby syntax highlighting, electric mode and some useful techniques to run code you are editing. This time I am going to describe how to install a full blown Rails development environment, that includes code snippets, and much more. Note that the Emacs-rails package requires Emacs 22 or greater.

What you need

If you have been following along with my setup, copy snippet.el and find-recursive.el into your include directory (or somewhere on your emails load path). Extract the Emacs-rails tar file into the include directory too. That should create a new directory, emacs-rails.

Now for the necessary changes to the _emacs file:


; needed for rails mode
(require 'snippet)
(require 'find-recursive)
; The rails require needs to go after ECB
; otherwise it loads a new incompatible speedbar
(add-to-list 'load-path "C:/emacs-22.1/includes/emacs-rails")
(require 'rails)

One other thing to note – I needed to perform these includes in the _emacs AFTER my code to setup ECB. Emacs-rails seems to include a different version of the Speedbar, which breaks ECB.

What can emacs-rails do?

I have found the documentation to be a bit sketchy, but there is a good summary of the various snippets and some of the other commands available.

In my setup, rails-minor-mode started automatically when I browsed into one of my Rails projects using ECB. If for some reason it doesn’t start, you can start it with ALT-x rails-minor-mode.

Since I am using ECB, I don’t envisage using the commands to jump to models and controllers much but I will try them out and see how it goes!

The snippets are very useful. If you are running emacs with toolbars enabled (the default) a new snippets menu will appear when in Rails mode, which you can use to insert them. Better is to learn the short codes for the snippets you use often. For example, typing:


flash

and hitting the tab key will result in the following appearing:


flash[:notice] = 'Successfully'

Each of the placeholder fields are highlighted, so you can hit TAB to get into each of them and just start typing to replace them – its easier to play with than to explain!

There are a host of other menu options available when in Rails mode that allow you to view the log files and start the webserver among other things. I have only just scratched the surface myself and am still learning, so I cannot say much more at the moment!

Update – A few months later

After using Ruby mode and Rails mode for a few months, something I could just not get used to was Electric mode. I can live with the automatically inserted ‘end’ keys words when I open a new method def or an if statement, but single character completions like a closing quote or curly bracket just frustrated me. You still have to type ctrl-f to skip forward a character to get past that auto-inserted quote, or use your arrow keys – its easier to just type the quote!

Different people will prefer different things, so to customise just how much electric you want Emacs to provide you can edit the ruby-electric.el file. Look for a section like the following:

(defun ruby-electric-setup-keymap()
  (define-key ruby-mode-map " " 'ruby-electric-space)
;;  (define-key ruby-mode-map "{" 'ruby-electric-curlies)
;;  (define-key ruby-mode-map "(" 'ruby-electric-matching-char)
;;  (define-key ruby-mode-map "[" 'ruby-electric-matching-char)
;;  (define-key ruby-mode-map "\"" 'ruby-electric-matching-char)
;;  (define-key ruby-mode-map "\'" 'ruby-electric-matching-char)
;;  (define-key ruby-mode-map "|" 'ruby-electric-bar))
  )

Each line provides electric mode for each of the different characters – I commented out all the lines starting with ‘;;’ (as ; denotes a commented line), which means that now I only get electric ‘end’ keywords added and nothing else.

Summary

In this series of five articles, I have explained why I decided to try Emacs, how to install it on windows and the Mac, setup ECB, some Ruby specific tips and finally this article to get the Rails development environment working. Hopefully you found them useful! At this point I am still a relative Emacs newbie – I don’t know much beyond what is in these posts, but I am learning something new almost every day. For the sake of completeness, the contents of my .emacs is below.


; sets emacs to prompt on close so you cannot close by accident!!
;(setq confirm-kill-emacs 'yes-or-no-p)
(global-font-lock-mode 1)

; directory to put various el files into
(add-to-list 'load-path "C:/emacs-22.1/includes")

; loads ruby mode when a .rb file is opened.
(autoload 'ruby-mode "ruby-mode" "Major mode for editing ruby scripts." t)
(setq auto-mode-alist  (cons '(".rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist  (cons '(".rhtml$" . html-mode) auto-mode-alist))

(add-hook 'ruby-mode-hook
          (lambda()
            (add-hook 'local-write-file-hooks
                      '(lambda()
                         (save-excursion
                           (untabify (point-min) (point-max))
                           (delete-trailing-whitespace)
                           )))
            (set (make-local-variable 'indent-tabs-mode) 'nil)
            (set (make-local-variable 'tab-width) 2)
            (imenu-add-to-menubar "IMENU")
            (define-key ruby-mode-map "C-m" 'newline-and-indent)
	    (require 'ruby-electric)
            (ruby-electric-mode t)
            ))

; These lines are required for ECB
(add-to-list 'load-path "C:/emacs-22.1/plugins/eieio-0.17")
(add-to-list 'load-path "C:/emacs-22.1/plugins/speedbar-0.14beta4")
(add-to-list 'load-path "C:/emacs-22.1/plugins/semantic-1.4.4")
(setq semantic-load-turn-everything-on t)
(require 'semantic-load)

; This installs ecb - it activated with M-x ecb-activate
(add-to-list 'load-path "C:/emacs-22.1/plugins/ecb-2.32")
(require 'ecb-autoloads)

(setq ecb-gzip-setup (quote cons))
(setq ecb-layout-name "left14")
(setq ecb-layout-window-sizes (quote (("left14" (0.2564102564102564 . 0.6949152542372882) (0.2564102564102564 . 0.23728813559322035)))))
(setq ecb-source-path (quote ("c:/rails")))

; needed for rails mode
(require 'snippet)
(require 'find-recursive)
; The rails require needs to go after ECB
; otherwise it loads a new incompatible speedbar
(add-to-list 'load-path "C:/emacs-22.1/includes/emacs-rails")
(require 'rails)

July 3, 2007 at 4:56 pm 6 comments

Emacs Ruby Foo

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

In my last post I described how I configured emacs to have a Textmate style code browser pane. Since then I have been doing some Rails hacking and have used it a lot and found it works well. In this post I am going to describe some Ruby specific customisations.

Let there be Syntax Highlighting

For most languages, Emacs will syntax highlight your source code right out of the box. It does this by switching into a language specific mode when you open files with certain extensions. Ruby Mode is not present any of my Emacs installs out of the box, but adding support is trivial.

ruby-mode.el

In the Ruby source distribution, under the misc directory, you will find several files. The one called ruby-mode.el is the one that adds Ruby syntax highlighting support to Emacs.

Download it, place it somewhere in your Emacs load path. On windows I created a directory called C:\emacs-22.1\includes and copied ruby-mode.el into it.

(Edit – While you’re there, download inf-ruby.el and put it in the same directory – its needed later when setting up Rails mode)

First ensure the ‘includes directory’ is in the emacs load path by defining it in your _emacs file (at or near the top as it needs to be declared before you use anything in it):


; directory to put various el files into
(add-to-list 'load-path "C:/emacs-22.1/includes")

Now add the following lines to your _emacs:


; loads ruby mode when a .rb file is opened.
(autoload 'ruby-mode "ruby-mode" "Major mode for editing ruby scripts." t)
(setq auto-mode-alist  (cons '(".rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist  (cons '(".rhtml$" . html-mode) auto-mode-alist))

Now when you open a file with an rb extension, ruby-mode will automatically activate. Also, if you open any rhtml files, html-mode will activate which is useful in Rails apps.

Adding Electric

Before saying anything more about ruby-mode, we may as well install ruby-electric mode. I am still in two minds if I like this feature or not, but I am living with it for now. Basically electric-mode defines ‘electric keys’. For example, if you type a quote, the closing quote will automatically be inserted. Same for {, [, and (. Also typing ‘def ‘ will result in the corresponding end being inserted too. Its easier if you play with it a little to see what I mean!

To install electric-mode, download the ruby-electric.el file from the Ruby misc directory into your include directory, and add the following to your _emacs:


(add-hook 'ruby-mode-hook
          (lambda()
            (add-hook 'local-write-file-hooks
                      '(lambda()
                         (save-excursion
                           (untabify (point-min) (point-max))
                           (delete-trailing-whitespace)
                           )))
            (set (make-local-variable 'indent-tabs-mode) 'nil)
            (set (make-local-variable 'tab-width) 2)
            (imenu-add-to-menubar "IMENU")
            (define-key ruby-mode-map "C-m" 'newline-and-indent) ;Not sure if this line is 100% right but it works!
            (require 'ruby-electric)
            (ruby-electric-mode t)
            ))

Now, we have Ruby Syntax highlighting, electric keys, and auto-indenting, but we can do more …

Messed up you indentation?

If you let you code get into a bit of a twist, you can fix the indenting by press the TAB key while on the line – give it a try, moving over each line and pressing TAB on each to correct the indents!

Comment a block

If you want to comment out a large section of code, you can select it and the type ALT-x ruby-encomment-region and it will place a ‘#’ symbol at the beginning of each line. ALT-x ruby-decomment-region does the opposite.

Compile and Run Code in Emacs

There are many ways to run code in Emacs. The easiest out-of-the-box way, is to type ALT-x compile. Emacs will prompt for a compile command, which by default is ‘make -k’. Replace this with:

ruby -w my_file_name.rb

The emacs window will split in two and the results of running the script will be in the new window.

If you are writing a text document that includes code snippets, and you want to ensure they are correct, you can select the region and type ALT-| to run it. Again, you need to enter the ruby command to run the code.

Smarter Compile?

All that typing ruby -w filename will start to annoy you after a while – the solution is mode-compile which has some brains built in. It can tell when you are editing a Ruby file (or many other types of file) and run it with the correct compiler/interpreter automatically. Download mode-compile.el and put it in your includes directory. As usual, add the following to your _emacs:


; Install mode-compile to give friendlier compiling support!
(autoload 'mode-compile "mode-compile"
   "Command to compile current buffer file based on the major mode" t)
(global-set-key "C-cc" 'mode-compile)
(autoload 'mode-compile-kill "mode-compile"
 "Command to kill a compilation launched by `mode-compile'" t)
(global-set-key "C-ck" 'mode-compile-kill)

Now you can compile/run code by typing CTRL-c c (and if your code enters a nasty infinite loop, you can kill it with CTRL-c k). The output will again appear in a new split window. If there are any compile errors in the output, you can move the cursor over them and hit return and emacs will jump to the offending line in your source file – pretty neat eh?

That is enough for this post – next time I will add Rails snippets and possibly irb support into our Emacs install.

June 21, 2007 at 6:16 pm 11 comments

The Emacs Code Browser

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

This is the third post in a series where I have been talking about why I decided to learn emacs and getting started with it on Windows or OS X. In this post, I will explain some of the customisations I made to fulfil my Rails development wishlist.

File Browsing

Textmate boasts an impressive feature, which is a file browsing pane, giving you easy access to all the files in your project. Luckily Emacs has something similar, called the Emacs Code Browser (ECB), but you have to figure out how to install it, which is not difficult, but it takes some time.

Downloads

Unfortunately, ECB relies on the presence of three other Emacs plugins so the first thing is to gather up all the files you need.

  • Semantic is used to help build text parsing applications in Emacs. Don’t worry about it beyond installing. I grabbed semantic-1.4.4 from here.
  • Eieio is also required. Again, I am not sure what its for beyond being required. I got eieio-0.17 here.
  • Speedbar is a code browser, that is also used by ECB. I downloaded speedbar-0.14beta4 here.
  • Finally, get ECB itself

Installing

Now we have the 4 downloads, we need to put them somewhere. On Windows, I created a directory called plugins inside my emacs directory, giving me C:\emacs-22.1\plugins

On the Mac, I created a directory called .emacs_includes/plugins in my home directory.

I am pretty sure Emacs gurus would tell me these files should go somewhere else, but this works for me and the location is not really important. Extract each of the 4 downloads into the plugins directory.

Installing Emacs plugins generally involves putting some files in a known location, and telling Emacs about them using the .emacs file. Open your .emacs file (or _emacs see the last post for more information) and add the following (this is a Windows example, edit the paths for OS X):

 
;Allows syntax highlighting to work, among other things
(global-font-lock-mode 1)
;These lines are required for ECB
(add-to-list 'load-path "C:/emacs-22.1/plugins/eieio-0.17")
(add-to-list 'load-path "C:/emacs-22.1/plugins/speedbar-0.14beta4")
(add-to-list 'load-path "C:/emacs-22.1/plugins/semantic-1.4.4")
(setq semantic-load-turn-everything-on t)
(require 'semantic-load)
; This installs ecb - it is activated with M-x ecb-activate
(add-to-list 'load-path "C:/emacs-22.1/plugins/ecb-2.32")
(require 'ecb-autoloads)

Loading ECB

Restart Emacs and start ECB by typing ALT-x ecb-activate

The Emacs window will change, adding a new section with 4 windows on the left side. In this default mode, the top window shows directories, next files, then history and finally methods in the current file.

To close ECB enter ALT-x ecb-deactivate

I changed this default layout to show only a combined directory and file listing and opened file history by adding the following lines to my .emacs:


(setq ecb-layout-name "left14")
(setq ecb-layout-window-sizes (quote (("left14" (0.2564102564102564 . 0.6949152542372882) (0.2564102564102564 . 0.23728813559322035)))))

The final thing you need to do, is add a list of directories containing the files you wish to browse, for example:


(setq ecb-source-path (quote ("d:/myRailsProject" "d:/useful scripts")))

There is a lot to configure in ECB if you wish. All the details are available in the packaged manual, which you can view by typing ALT-x ecb-show-help.

Some commands I have learned so far include:

  • Jump to the directory window CTRL-. gd
  • Jump to the history window CTRL-. gh
  • Jump to the last window you were in CTRL-. gl
  • Jump to the first editor window CTRL-. g1

The directory browser can be controlled without using the mouse too – just use the arrow keys and enter – give it a go!

As you can see, setting up ECB is not difficult, and its well worth it in my opinion. In the next article, I will add some Ruby customisations to our Emacs installation.

June 13, 2007 at 4:33 pm 15 comments

Older Posts Newer Posts


Feeds