article was published on Sun Developer's web site at: http://developers.sun.com/solaris/articles/external_linkage.html
Apparently I got the idea to submit this article from my blog post:
http://technopark02.blogspot.com/2005/01/cc-external-linkage-with-extern-c.html
Wednesday, 23 March 2005
Sunday, 13 March 2005
Sun Studio: Investigating memory leaks with Collector/Analyzer
Sun Studio compiler collection bundles a set of tools for collecting and analyzing performance data from an application. Collector & Analyzer, a pair of tools from Sun Studio suite that we use to collect and analyze the performance data. Both tools can be used from the command line or from a graphical user interface.
These two tools help answering the following questions:
From the code, it is clear that the program is requesting memory allocations four times (a total of 725 bytes); but releasing only 25 bytes from virtual memory. The following steps explain how to find this 700 byte leak with Collector/Analyzer.
These two tools help answering the following questions:
- How much of the available resources does the program consume?
- Which functions or load objects are consuming the most resources?
- Which source lines and instructions are responsible for resource consumption?
- How did the program arrive at this point in the execution?
- Which resources are being consumed by a function or load object?
% more mem.c
#include <stdlib.h>
#include <stdio.h>
void allocate() {
int *x;
char *y;
x = (int *) malloc(sizeof(int) * 100);
y = (char *) malloc (sizeof(char) * 200);
printf("\nAddress of x = %u, y = %u", x, y);
x = (int *) malloc (sizeof(int) * 25);
y = (char *) malloc (sizeof(char) * 25);
printf("\nNew address of x = %u, y = %u\n", x, y);
free (y);
}
int main() {
allocate();
return (0);
}
From the code, it is clear that the program is requesting memory allocations four times (a total of 725 bytes); but releasing only 25 bytes from virtual memory. The following steps explain how to find this 700 byte leak with Collector/Analyzer.
- Prepare the program for data collection
To see source code in annotated source and disassembly, and source lines in the line analysis, the source file must be compiled with -g compiler option to generate debug symbol information
% CC -g -o mem mem.c - Collect the data
We can collect run-time data that is useful for later analysis by running the executable undercollectutility. To see the entire list of arguments thatcollectaccepts, typecollectwithout any arguments on the command line. Since we were interested in memory leaks, we have to turn the heap tracing on with -H option. (By default, heap tracing is off). Also we have to supply an experiment name to hold the data with -o option
% which collect
/opt/SUNWspro/prod/bin/collect
% collect -H on -o mem.er mem
Creating experiment database mem.er ...
Address of x = 134743584, y = 134743992
New address of x = 134744200, y = 134614672
It creates the mem.er experiment and archives the binary files describing each load object referenced in the loadobjects file - Finally invoke the performance analyzer, er_print (command line tool) or analyzer (graphical tool)
% er_print mem.er
(er_print) source allocate
Source file: ./mem.c
Object file: ./mem
Load Object: ./mem
Incl. Incl. Excl. Incl.
Bytes Leaks User CPU User CPU
Leaked sec. sec.
1. #include <stdlib.h>
2. #include <stdio.h>
3.
0 0 0. 0. 4. void allocate() {
0 0 0. 0. 5. int *x;
0 0 0. 0. 6. char *y;
7.
400 1 0. 0. 8. x = (int *) malloc(sizeof(int) * 100);
200 1 0. 0. 9. y = (char *) malloc (sizeof(char) * 200);
10.
0 0 0. 0. 11. printf("\nAddress of x = %u, y = %u", x, y);
12.
100 1 0. 0. 13. x = (int *) malloc (sizeof(int) * 25);
0 0 0. 0. 14. y = (char *) malloc (sizeof(char) * 25);
15.
0 0 0. 0. 16. printf("\nNew address of x = %u, y = %u\n", x, y);
0 0 0. 0. 17. free (y);
0 0 0. 0. 18. }
19.
0 0 0. 0. 20. int main() {
## 700 3 0. 0. 21. allocate();
0 0 0. 0. 22. return (0);
0 0 0. 0. 23. }source allocateshows the annotated source with line numbers and number of bytes leaked from each line since we collected the data with heap tracing on (otherwise it shows the total CPU time spent in each line, by default)
From the annotated source, we can see that 700 bytes were leaked after the execution of line:## 700 3 0. 0. 21. allocate();
. And from the source ofallocate(), the following lines were the culprits:
statement was clean, since we have
400 1 0. 0. 8. x = (int *) malloc(sizeof(int) * 100);
200 1 0. 0. 9. y = (char *) malloc (sizeof(char) * 200);
100 1 0. 0. 13. x = (int *) malloc (sizeof(int) * 25);
____________
0 0 0. 0. 14. y = (char *) malloc (sizeof(char) * 25);0 0 0. 0. 17. free (y);at the end of functionallocate().
We can also get much useful information with the help ofleaksandallocscommands that relate to memory allocations and deallocations. Brief description of those commands are as follows:- leaks
Display a list of memory leaks, aggregated by common call stack. Each entry presents the total number of leaks and the total bytes leaked for the given call stack. The list is sorted by the number of bytes leaked. - allocs
Display a list of memory allocations, aggregated by common call stack. Each entry presents the number of allocations and the total bytes allocated for the given call stack. The list is sorted by the number of bytes allocated.
- leaks
Saturday, 12 March 2005
Solaris 10: Installing MPlayer
MPlayer - Introduction:
MPlayer is a movie player for UNIX/Linux and plays most MPEG, VOB, AVI, OGG/OGM, ASF/WMA/WMV, QT/MOV/MP4, RM etc., files, supported by many native, XAnim, RealPlayer, and Win32 DLL codecs. You can watch VideoCD, SVCD, DVD, RealMedia, and MPEG-4 (DivX) movies too
Prerequisites:
GNU tools: gmake (make), gcc (compiler), gas (assembler), libiconv etc.,
Note:
Building MPlayer
Suggested Reading:
MPlayer is a movie player for UNIX/Linux and plays most MPEG, VOB, AVI, OGG/OGM, ASF/WMA/WMV, QT/MOV/MP4, RM etc., files, supported by many native, XAnim, RealPlayer, and Win32 DLL codecs. You can watch VideoCD, SVCD, DVD, RealMedia, and MPEG-4 (DivX) movies too
Prerequisites:
GNU tools: gmake (make), gcc (compiler), gas (assembler), libiconv etc.,
Note:
- On Solaris SPARC, we need the GNU C/C++ Compiler; it does not matter if GNU C/C++ compiler is configured with or without the GNU assembler
- On Solaris x86, we need the GNU assembler and the GNU C/C++ compiler, configured to use the GNU assembler. The MPlayer code on the x86 platform makes heavy use of MMX, SSE and 3DNOW! instructions that cannot be compiled using Sun's assembler /usr/ccs/bin/as
- Error message from configure on a Solaris x86 system using GCC without GNU assembler
:
% configure
...
Checking assembler (/usr/ccs/bin/as) ... , failed
Please upgrade(downgrade) binutils to 2.10.1... - Typical error we get when building with a GNU C compiler that does not use GNU assembler (gas):
% gmake
...
gcc -c -Iloader -Ilibvo -O4 -march=i686 -mcpu=i686 -pipe -ffast-math
-fomit-frame-pointer -I/usr/local/include -o mplayer.o mplayer.c
Assembler: mplayer.c
"(stdin)", line 3567 : Illegal mnemonic
"(stdin)", line 3567 : Syntax error
... more "Illegal mnemonic" and "Syntax error" errors ...
Building MPlayer
- Download the latest source from http://www.mplayerhq.hu/homepage/design7/dload.html and extract the files
- Check the PATH and make sure that GNU tools are available in PATH
./configure --prefix=<installation directory for mplayer> --as=gasin the directory where you extracted the files. This is where you see the file "configure" and "src" directory
This step creates a bunch of Makefile(s) in different folders with the information that we supplied during "configure"gmake
It compiles the code and creates the executables and shared objects or static libraries (depending on options supplied to configure)- Finally,
gmake install
This command installs the mplayer, dependencies in appropriate places. You may have to use the Sun audio driver with the-ao sunoption for sound
eg., Playing an mpeg-1 file called Matrix.mpgmplayer -ao sun Matrix.mpg
Suggested Reading:
- MPlayer's documentation http://www.mplayerhq.hu/DOCS/HTML/en/index.html &
- MPlayer documentation for Solaris port: http://www.mplayerhq.hu/DOCS/HTML/en/solaris.html
Sunday, 6 March 2005
Solaris: Setting up a DHCP client
If the machine is connected to the network during the installation of the OS (Solaris in this case), the operating system takes care of setting up the dhcp (Dynamic Host Configuration Protocol) client for us. But if the machine is not hooked up to a network during the installation, we may have to set it up on our own once the machine is ready to use the network services. Infact it is a very simple process to setup and run dhcp client, which in turn takes care of getting the dynamic IP, DNS etc., from a DHCP server
All we need to do is to create couple of files for the ethernet interface.
Acknowledgements & suggested reading:
All we need to do is to create couple of files for the ethernet interface.
- To get the interface name for ethernet, type dmesg | grep ether. Let's assume that it is elxl0
- Create an empty file called /etc/hostname.<ether interface> eg., touch /etc/hostname.elxl0
Creating this file ensures that the interface gets plumbed. By plumbing, it implements the TCP/IP stack ie., the OS sets up the streams needed for TCP/IP to use the device and makes it ready for the DHCP software to do its stuff
Behind the scenes: During the boot process, the OS reads all the /etc/hostname.* files and plumbs the devices. Once plumbed, the devices are configured by reading the /etc/hosts and the /etc/netmasks file - Next step is to create /etc/dhcp.<ether interface>. eg., touch /etc/dhcp.elxl0
This file can be empty if you want to accept the defaults, but may also contain one or both of the following directives:- wait time, and
- primary
By default, ifconfig will wait for 30 sec for the DHCP server to respond and then the boot process continues, while the interface gets configured in the background. Specifying the wait directive tells ifconfig not to return until the DHCP has responded. The primary directive indicates to ifconfig that the current interface is the primary one, if you have more than one interface under DHCP control. If you have only one interface under DHCP control, then it is automatically the primary one; so primary is redundant, although it's permissible
Acknowledgements & suggested reading:
- Setting up a Solaris DHCP client by Rich Teer
- Configuring network interface cards by Lance Spitznet
- Solaris DHCP client at Sun Solaris documentation web site
Friday, 4 March 2005
UNIX/Linux: Activating Comcast High Speed Internet Account
Fact: Comcast doesn't support any OS except Windows & Mac. It doesn't mean that Unix/Linux users can not access internet with Comcast services; but it means Comcast refuses to trouble shoot any issues on *nix platform ie., *nix users are on their own to fix any of the issues
In general, Comcast technician leaves a CD-ROM to activate the Comcast account, once the installation is done. The activation wizard of the CD takes care of registering the modem, activating the account for us. Unfortunately it works only on Windows and Mac platforms
However Comcast is kind enough to provide web interface for activating the account (some people refer it as "registering the modem"; Comcast refers it as "Activating the ISP"). Comcast uses DHCP (Dynamic Host Configuration Protocol) for IP assignment. So, if we have a DHCP client running on the machine, it contacts the Comcast's DHCP server(s) and obtains a valid IP, DNS info etc., ie., As soon as the cable modem installation is done, our machine will be on the network (to verify, simply try arp <any well known hostname>); but we can't actually access any of the public web sites due to the pending registration. Since we are in the network of Comcast, we will be able to access few web sites of Comcast, with the help of Comcast's proxy server. URLs for Comcast's primary proxy servers are as follows:
Activation steps are as follows:
PS:
The above steps are equally applicable for other platforms as well; but Unix, Linux were explicitly added to the title for Google feed
Thanks to:
James Dean Palmer
In general, Comcast technician leaves a CD-ROM to activate the Comcast account, once the installation is done. The activation wizard of the CD takes care of registering the modem, activating the account for us. Unfortunately it works only on Windows and Mac platforms
However Comcast is kind enough to provide web interface for activating the account (some people refer it as "registering the modem"; Comcast refers it as "Activating the ISP"). Comcast uses DHCP (Dynamic Host Configuration Protocol) for IP assignment. So, if we have a DHCP client running on the machine, it contacts the Comcast's DHCP server(s) and obtains a valid IP, DNS info etc., ie., As soon as the cable modem installation is done, our machine will be on the network (to verify, simply try arp <any well known hostname>); but we can't actually access any of the public web sites due to the pending registration. Since we are in the network of Comcast, we will be able to access few web sites of Comcast, with the help of Comcast's proxy server. URLs for Comcast's primary proxy servers are as follows:
- https://sas.r1.attbi.com serves Oregon & Washington regions
- https://sas.r2.attbi.com serves California except Sacramento
- https://sas.r3.attbi.com serves Texas
- https://sas.r4.attbi.com serves Utah, Colorado, Wyoming, Montana & Sacramento (CA)
- https://sas.r5.attbi.com serves Indiana, Illinois & Michigan
- https://sas.r6.attbi.com serves Ohio, Connecticut & Pennsylvania
Activation steps are as follows:
- Configure your browser to use the Comcast's proxy server for your geographic region (see above for the list)
- HTTP & SSL proxies must be configured to use the proxy server of your region
- 8000 is the port for both HTTP & SSL proxies
- Fill out "No Proxy for:" field with the name of the proxy server for your region without the string "https://sas." from the actual URL
- HTTP & SSL proxies must be configured to use the proxy server of your region
- Now try to access the proxy server URL for your region. Hopefully you should be able to see a page with two fields: Account Number and Registration Code. Account Number is the Subscriber Number and Registration Code is the number (ID) that you may find under "Service Name and Address" field of the invoice you received from Comcast.
- As you proceed to the next step, you will be also be prompted to setup your Comcast e-mail account
- Once the device (modem) is registered, the web page shows a link to restart the cable modem. Go ahead and restart the modem. If you are using a router, you need to restart the modem as well. To restart the router: turn the power off, wait for a min and then turn it back on.
- Now your account is activated; but you still can not access public web sites due to Comcast's proxy server configuration. Since we do not need it anymore, go back to web browser's proxy configuration window and choose "Direct connection to the internet"
- If you can not access internet even at this point, try to restart the network or the machine depending on your expertise level. Even if it didn't help, try calling Comcast support center and find out the the proxy server, that you need to use in your region
PS:
The above steps are equally applicable for other platforms as well; but Unix, Linux were explicitly added to the title for Google feed
Thanks to:
James Dean Palmer
Thursday, 3 March 2005
Obfuscated "C" code
Found this interesting piece of C code somewhere on internet. Of course it compiles and prints out 3.141 on console; And it is not so hard to guess how the code in F_OO() gets executed
Govinda% more fun.c
#include <stdio.h>
#define _ F-->00 || F-OO--;
long F=00,OO=00;
void F_OO() {
_-_-_-_
_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_
_-_-_-_
}
main(){
F_OO();
printf("%1.3f\n", 4.*-F/OO/OO);
}
Govinda % cc -o fun fun.c
Govinda % ./fun
3.141