Category: linux


OpenOffice File Menu “Randomly” Pops Up on Ubuntu.

If you are one of the few unfortunate blokes that’s pulling your hair out over random File Menu popups on OpenOffice — Word or Spreadsheet — and have been hitting your head against a brick wall trying to find the solution, I hear you.

I intially thought there was some interaction with Skype, but now I have to retract that statement. In all honesty, I have no clue what the problem is.

But I decided to just watch the “random” File Menu problem and time it. And now I am more confused than ever.

On my computer running 64-bit Ubuntu 9.10 (Karmic), I noticed that the File Menu toggles twice a second when OpenOffice has the focus. One toggle event happens precisely on the 11th second; the other toggle event happens around 35th to the 45th second. The second toggle seems to be related to when I launch OpenOffice; the first is always precisely on the 11th second.

And this is something peculiar to OpenOffice; I have not observed this problem with any other program.

And the problem did go away for a while, but came back. I have no explanation for that, either.

But the precise timing of this bug may have something to do with an internal timer within OpenOffice, but I am just guessing at this point. I strongly suspect at this point it’s completely an OpenOffice problem, i.e. no interaction with anything else.

This weirdness was observed on Ubuntu 9.10 (Karmic Koala) 64-bit, OpenOffice 3.1.1. Please drop a line here if you observe this bizarre behavior. Give your timings if you can; may lend more insight as to the nature of this problem.


The Difference Between Multithreaded and Multicore Programming

It is no joke that computer hardware has advanced by leaps and bounds over the past decade. 10 years ago, multicore systems were expensive and high-end; today, your grandmother may have one and probably have no clue what she has!

Alas, application software has not kept pace. The Linux OS has done a fair job at being able to leverage some of the power multicore systems offer us, but applications running on them have not. The same can be said more or less for Windows, but it’s been a long while since I did anything systems-level with Windows. But the same issues do apply, however.

We are today with the multicore situation where we were in the 80’s and the 90’s with the multithreaded issues. Back then, CPUs grew support for multithreaded programming, but software — including some OSes — were slow to adopt. The Macintosh, when it was first released in 1984, would only support “cooperative” multitasking when the underlying 68000 was perfect for preemption, which was leveraged by the Amiga that came out a year later in 1985.

Even I wrote a preemptive multitasking OS back in 1980, when I was 18 years old! I had a hard time understanding why Apple and Microsoft were having such a hard time with this 5 years later. The first preemptive OS released by Microsoft was Windows NT, that came out in the late 80’s, shortly after the time Commodore closed its doors.

I to this day am still mystified why big corporations like Apple and Microsoft couldn’t pull off until much later what an 18 year-old such as myself could do in a couple of months!

Today, we are in a similar situation with applications — especially those applications that should be able to leverage multicore power such as Databases and Games and Web Servers. Actually, Web Servers such as Apache doesn’t fair too bad in this area from what I can tell. Can’t speak to IIS, since I’ve never used it. The MySQL Database struggles to be able to leverage the multicore, but the developers there seem to only take incremental steps. For a while MySQL’s performance used to actually degrade on multicore platforms! Now it’s been optimized for at least 8 cores, but you still may not see the expected gain on 16 or 32-core systems.

Why would this be the case? Surely, MySQL and other applications are written to be *multithreaded*. Ah, there lies the rub. Without getting deep into the details of system resource allocation, spin-locking, and the like, I wish to discuss this issue from a “high-level” perspective. I’m sure some of you not-so-tech-savvy tech managers would appreciate that!

And, by the way, it’s nothing wrong with not understanding all the nitty-gritty details of semaphores and synchronization issues if you are a tech manager. At least, as long as your people do. But then, that’s part of the problem. Some do; many don’t. And those that don’t may not be as forthcoming as you’d like for fear of being fired or ridiculed or being given a lower status, etc. That’s the way it is.

From the bird’s point of view, it’s not too hard to understand at all. The basic difference in multithreaded programming and multicore programming is this:

If you are doing multithreaded programming for single-core systems, you gain nothing for trying to do a lot of processing simultaneously in multiple threads. The goal of multithreaded programming is to, instead, keep the idle time of the CPU as low as possible whilst doing the most work possible where the work will be done in serial fashion anyway. If you actually do attempt to do real processing in multiple threads, in most cases your performance will actually degrade faster than if you did it using serial programming. Why? Because the processor takes time to context-switch between tasks, and the more it has to do that, the more overhead you’ll incur.

On a multicore system, on the other hand, your goals are quite different — you actually do want to spread the work out among the cores so that it DOES executes simultaneously, because your performance gain should be directly related to how many cores you have. So, a quad core system should be able to get the work done 4 times faster than a single core system. A 16-core platform should be 4-times faster than a quad-core system, and 16-times faster than a single core.

Ah, but as always, there’s a catch. Hence my use of the words, “should be”, rather than “will be”.

Making efficient use of multiple cores in highly non-trivial. For starters, you may not be able to break the tasks down to a parallelizable form. Or if you can, there may still be dependencies between the tasks where one would have to wait on another for information, or wait for a common resource — such as the hard drives or network cards — to become available.

In a dynamic situation, such as a database server, the issues can become even more convoluted as you deal with the order many resources — such as rows in a table — are locked by many tasks running on many cores.

If you are having to deal with legacy code not designed for multicore systems, as was the case with the MySQL codebase, the issues becomes even hairier.

Also, most languages in popular use, such as C++, Java, Python and Ruby, have little to no facillities for multicore or distributed programming.  Interpreted scripting languages like Python don’t even handle multithreading very well, at least Python pre 3.0. Ruby has issues in this regard as well.

The common wisdom with some is to run your program in multiple processes, which does work for those situations that doesn’t require a lot of state dependencies or resource sharing. That approach, when it works, is nice, because it scales well with the number of cores you have — and it also scales well in a cluster/cloud computing scenario.

But if those simultaneously running systems DO require a lot of sharing of data, resources, and other dependencies, the scale factor is severely restricted. It may call for a complete rework of the algorithms involved or some clever system hacks, or both.

I don’t think there is any common wisdom that can be applied in all cases. And when you are dealing with time-to-market constraints, budgetary realities, and the like, you may be forced to take a sub-optimal path.

So it may be safe to say that it may be a little while before we see software truly leverage the true power of multicore systems. We will see it here and there where the effort can be applied and the understanding is present, but the rest will entail a slow evolutionary process.

MIT offers a course on multicore programming. Can’t say how good or bad it is, but it’s MIT. How can you go wrong? :-)


Of Character Sets, MySQL, and localization woes…

Let’s say you need to do a website that must support multiple languages for cultures as diverse as Japan, France, Russia, Saudi Arabia, and Brazil, as well as the US. This can be quite a daunting task, with all kinds of unexpected gotchas.

The ideal character set of choice is, of course, UTF8. Alas, you will note that most of the systems you’ll need to use defaults to LATIN1, including MySQL. If your site is written in PHP, that also by default is set to LATIN1.

I find it quite puzzling that in this day and age of globalization that many of the tools don’t default to UTF8. And there are major issues with this, because everything in the chain of delivery must either be set to UTF8 or can handle UTF8 or you’ll see bizarreness when you attempt to display the characters of some languages. You will probably see a series of question marks (”??? ??? ?????”) instead of the actual words. Sometimes you may see a series of squares. Or maybe it looks like total garbage.

To debug charset issues, you must be certain that everything in the delivery chain is set for UTF8. I can’t stres this enough.

For example, on one project, the MySQL database was properly set to UTF8, but we kept seeing LATIN1 creep in from somewhere. The site was driven by PHP, and we made sure PHP was set to UTF8, but there were still issues. It turned out that PDO/mysqli was still defaulting to LATIN1, which was revealed by looking at the results of the following query issued through PHP:

SHOW VARIABLES LIKE “character%”;

Which should result in:

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

But instead we saw:

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

Clearly demonstrating that there was a connection issue. However, we were able to, as a quick fix, issue the following query on that connection:

SET NAMES ‘utf8′;

Which fixed the problem, though requiring us to run that query on every new connection. I am sure there is a better approach, but we didn’t have time to find it.

But to give you an idea, this is the chain we had to check for UTF8:

  • MySQL Server
  • MySQL Driver/PDO Wrapper
  • PHP
  • Browser

If you are interacting with MySQL through the command-line client, then make sure you launch it thusly:

mysql –default-character-set=utf8

Or have the appropriate settings in the [client] section of my.cnf.

The character set headaches are not just limited to MySQL, but any interacting systems, web services, etc. Carefully checking the chain to ensure that every part of that chain defaults to UTF8 is essential to saving the day for the world of localized globalization!


KDE Konsole Backgrounds and ssh

If you are a GUI-oriented person, you need not read this. But if you are like me, you make heavy use of the console. If you are managing many machines as well as your own Linux workstation, it’s VERY important to know where your console session is.

Too many times in the past I had wanted to bring down my workstation, and would type “shutdown” or “reboot” in the console window, only to find out to my horrors that the console was really a remote session to one of my web servers serving up hundreds of web sites.

Whoops!

Well, that prompted me into developing a solution where I can tell at a glance where I happened to be logged in. This way,  I wouldn’t be in danger of issuing dangerous commands on the wrong server. And if you are working for someone else, it also keeps you from being FIRED!

I use KDE to do my development and administration, and I have fallen in love with Konsole. Konsole, despite its quirks, has a lot of nice features that makes it a shoe-in for what I am talking about.

My approach now is to create login bash scripts to begin a session with whatever machine I need to ssh into, and have that script also do something nice to the Konsole background in the process.

I also develop a lot of websites, as well as other things. Sometimes, it’s helpful to change the background when I go into emacs so that I have the proper contrast for syntax coloring, etc. Same approach works there as well.

kschemaset_konsoles.png The secret to my machinations? A little script I wrote called kschemaset. It does all the “magic” in  resetting the Konsole background, and is actually derived from a similar script that didn’t do everything I needed. But in the fine tradition of opensource, I grabbed it and enhanced it over time. It currently does require you to either create images or acquire images for your backgrounds, but eventually I wish  to create a more comprehensive opensource package that will do all that magic for you automatically. But for now, it’s a lot of fun to come up with a cute background that represents the server you are working on! I recommend choosing either very light pastel colors or very dark colors with low contrast, because you want to be able to read the text without killing your eyes.

You don’t have to create any artwork at all, actually, and many may elect to do it that way. It’s all up to how you want to proceed.

The first step is to install the kschemaset script somewhere where it can execute. There are actually other related scripts involved, and they are all available from here. Typically, I create a local bin directory to my login account and alter the .bashrc file to add it to the PATH environment variable. Since you are obviously well adept at such things if you are interested, I won’t bother with holding your hand here.

The next step involved is to create Konsole schemes to reflect the servers you wish to work on. All kschemaset does is invoke a schema for the duration of the session, and restore the old schema when the session is concluded. Eventually, not only do I want to automate the creation of new Konsole schemas, but also the images used for the backgrounds. But for now you can do this by hand — or automate these steps yourself. If you do, let me know so I don’t wind up reinventing your wheel!

Konsole has one annoying problem that seems not to have been fixed in recent times — when you add a new schema, any Konsoles that were open prior to the addition tend to get “confused” and may start displaying the wrong schema. The workaroud for this is to either to manually select the schema for those Konsoles out of sync, or to restart them. Hopefully this problem will be fixed soon.

Now, you simply create scripts based on kschemaset that will launch your new sessions, and change not only the background, but the tab text as well, on the fly, and to reset everything to the pre-existing schema and tab text when you’re done. I’ve even did this with emacs to give me a flat-black background to do my editing on.  The possibilities are endless.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash
# Set the schema of the currently-running Konsole
# Based on konsoledcopschema
 
 
# So running script can know this is where it's running
# (to enable scripts to enable kschemaset via recursion)
export kschemaset=1
 
. kconfuns
getKonsoleInfo
getAppInfo ${*}
shift
 
if [[ ! ${appSchema} ]] ; then
    echo "(schema $appName not found)"
fi
 
export -f ksetSchema getKonsoleInfo getAppInfo
 
if [ "${inKonsole}" == "1" ] && [[ ${appSchema} ]] ; then
    # dcop $konsole konsole reparseConfiguration
    dcop $konsole $session setSchema "${appSchema}"
    dcop $konsole $session setSchema "${appSchema}" # testing -- may not have gone through the first time
    if [ -n "${appName}" ] ; then
        dcop $konsole $session renameSession "${origSession}: ${appName}"
    fi
 
    if [ -n "${*}" ] ; then # run the command and reset to prveious schema.
        ${*}
        dcop $konsole $session setSchema "${origSchema}"
        dcop $konsole $session renameSession "${origSession}"
    fi
else
    kschemaset=2
    ${*}
fi

And now for the next script:

[-]?Download kconfuns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash
# Konsole functions. Include with ". kconfuns"
 
kloc=~/.kde/share/apps/konsole/
 
getKonsoleInfo()
{
    # Make sure we're in Konsole
    if [ -n "${KONSOLE_DCOP}" ] ; then
        export inKonsole='1'
        export konsole=`echo $KONSOLE_DCOP | cut -d\( -f 2 | cut -d, -f1`
        export session=`dcop $konsole konsole currentSession`
        export origSchema=`dcop $konsole $session schema`
        export origSession=`dcop $konsole $session sessionName`
    else
        export inKonsole='0'
    fi
}
 
getAppInfo()
{
    export appName="${1}"
    export appSchema=`ls -1 $kloc | egrep "${appName}\."`
}
 
ksetSchema()
{
    dcop $konsole $session setSchema $1.schema
}
 
ksetSessionName()
{
    dcop $konsole $session renameSession "$*"
}

And here is an example of lauching an ssh session using kschemaset:

[-]?Download polaris
1
2
3
#!/bin/bash
# Log on to the Polaris Web Server
kschemaset Polaris  ssh -t youraccount@yourserver.yourdomain.com $*

And it’s that simple!