<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel>
  <title>AMD Developer Blogs - AMD Java Labs</title> 
  <description></description> 
  <link>http://forums.amd.com/devblog/index.cfm?forumid=8</link>
  <language>en-US</language>
  <generator>FuseTalk Hosting Executive Plan 3.2 Build 80405</generator>

	<item>
		<dc:creator>Tom Deneau</dc:creator>
		<title>Java Generics Performance Puzzler Part 2</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=117461</link> 
		<pubDate>2009-08-14T15:32:25 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=117461#comments</comments>
		<trackback:ping>4</trackback:ping>
		<description><![CDATA[ <div style="margin: 0in 0in 10pt;">
<table class=" FCK__ShowTableBorders" style="width: 100%;" border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td style="background-color: transparent; border: #ece9d8; padding: 0in;" valign="top">
<table class=" FCK__ShowTableBorders" style="width: 100%;" border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td style="background-color: transparent; border: #ece9d8; padding: 0in;" valign="top">
<table class=" FCK__ShowTableBorders" style="width: 100%;" border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td style="border-bottom: #ece9d8; border-left: #ece9d8; padding-bottom: 0in; background-color: transparent; padding-left: 0in; padding-right: 0in; border-top: #8f8f8f 1pt solid; border-right: #ece9d8; padding-top: 4.5pt;">&nbsp;</td>
</tr>
</tbody>
</table>
<div style="margin: 0in 0in 10pt;"><span style="line-height: 115%; color: #000000; font-size: 10pt;">In a <a href="blogpost.cfm?threadid=114296&catid=313"><span style="color: #800080;">previous blog</span></a>, we looked at a microbenchmark where we were pulling an item from a collections class like an ArrayList and eventually&nbsp;putting it in another collection.&nbsp; And we saw that there could be a significant performance difference between the following two versions:</span></div>
<div style="margin: 0in 0in 10pt;"><em><span style="line-height: 115%; color: #000000; font-size: 10pt;">&nbsp;(Note: In the following examples, we show only the parts where we access the ArrayLists and leave out any subsidiary logic.)</span></em></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span style="color: #000000; font-size: 10pt;">ArrayList aListSrc, aListDest1, aListDest2;<br /><br /></span><strong><em><span style="text-decoration: underline;"><span style="color: #000000; font-size: 12pt;">Version&nbsp;1</span></span></em></strong></div>
<div style="line-height: 12pt; margin: 0in 0in 10pt 0.5in;"><span style="color: #000000; font-size: 10pt;">while (idxSrc &lt; NUMOBJS) {<br />&nbsp;&nbsp;&nbsp; aListDest1.add(idxDest, aListSrc.get(idxSrc++));<br />}</span></div>
<div style="line-height: 20.4pt; margin: 0in 0in 10pt;"><span style="color: #4c4c4c; font-size: 10pt;">and</span></div>
<div style="line-height: 20.4pt; margin: 0in 0in 10pt;"><strong><em><span style="text-decoration: underline;"><span style="color: #000000; font-size: 12pt;">Version&nbsp;2</span></span></em></strong></div>
<div style="line-height: 12pt; margin: 0in 0in 10pt 0.5in;"><span style="color: #000000; font-size: 10pt;">while (idxSrc &lt; NUMOBJS) {<br />&nbsp;&nbsp;&nbsp; MyClass myc = aListSrc.get(idxSrc++);<br />&nbsp;&nbsp;&nbsp; aListDest1.add(idxDest, myc);<br />}</span></div>
<div style="margin: 0in 0in 10pt;"><span style="line-height: 115%; color: #000000; font-size: 10pt;">with version 2 being slower because it requires a castcheck to check that the Object returned by aListSrc.get could be cast to a MyClass.&nbsp;The performance impact was because the castcheck required touching an object that did not need to be touched in version 1.</span></div>
<div style="margin: 0in 0in 10pt;"><span style="line-height: 115%; color: #000000; font-size: 10pt;">In the microbenchmark code above, we navigated thru the ArrayList by incrementing an integer index to the ArrayList.get method.&nbsp; What if we had used an explicit iterator or used the implied iterator in Java&rsquo;s &nbsp;for-each statement?</span></div>
<div style="margin: 0in 0in 10pt;"><span style="line-height: 115%; color: #000000; font-size: 10pt;">First let&rsquo;s look at the least cluttered implementation, which uses for-each loop</span></div>
<div style="line-height: 20.4pt; margin: 0in 0in 10pt;"><strong><em><span style="text-decoration: underline;"><span style="color: #000000; font-size: 12pt;">Version&nbsp;3</span></span></em></strong></div>
<div style="line-height: 12pt; margin: 0in 0in 10pt 0.5in;"><span style="color: #000000; font-size: 10pt;">for (MyClass myc : aListSrc) {<br />&nbsp;&nbsp; aListDest1.add(myc);<br />&nbsp;&nbsp; // ...<br />}</span></div>
<div style="margin: 0in 0in 10pt;"><span style="line-height: 115%; color: #000000; font-size: 10pt;">and remembering that the for-each loop is syntactic sugar for the following:</span></div>
<div style="line-height: 20.4pt; margin: 0in 0in 10pt;"><strong><em><span style="text-decoration: underline;"><span style="color: #000000; font-size: 12pt;">Version&nbsp;3b</span></span></em></strong></div>
<div style="line-height: 12pt; margin: 0in 0in 10pt 0.5in;"><span style="color: #000000; font-size: 10pt;">for (Iterator iter = aListSrc.iterator(); iter.hasNext() ) {<br />&nbsp;&nbsp;&nbsp; MyClass myc = iter.next();<br />&nbsp;&nbsp;&nbsp; //body of loop<br />&nbsp;&nbsp;&nbsp; aListDest1.add(myc);<br />}</span></div>
<div style="margin: 0in 0in 10pt;"><span style="line-height: 115%; color: #000000; font-size: 10pt;">we can see that, unfortunately, this suffers from the same castcheck as Version 2.&nbsp;&nbsp; And, once again, we cannot get around the castcheck by making the for-each variable an Object, because the compiler wisely will not let you add an Object to an ArrayList:</span></div>
<div style="line-height: 20.4pt; margin: 0in 0in 10pt;"><strong><em><span style="text-decoration: underline;"><span style="color: #000000; font-size: 12pt;">Version&nbsp;4 (will not compile)</span></span></em></strong></div>
<div style="line-height: 12pt; margin: 0in 0in 10pt 0.5in;"><span style="color: #000000; font-size: 10pt;">for (Object myc : aListSrc) {<br />&nbsp;&nbsp; aListDest1.add(myc);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // </span><span style="color: #000000; font-size: 10pt;">&szlig;</span><span style="color: #000000; font-size: 10pt;"> error here<br />}</span></div>
<div style="margin: 0in 0in 10pt;"><span style="line-height: 115%; color: #000000; font-size: 10pt;">Looking at the expanded code for the for-each loop, we see that we can still both use an explicit iterator&nbsp;and avoid the castcheck by getting rid of the temporary variable from Version 3b and ending up with something like the following:</span></div>
<div style="line-height: 20.4pt; margin: 0in 0in 10pt;"><strong><em><span style="text-decoration: underline;"><span style="color: #000000; font-size: 12pt;">Version&nbsp;5</span></span></em></strong></div>
<div style="line-height: 12pt; margin: 0in 0in 10pt 0.5in;"><span style="color: #000000; font-size: 10pt;">for (Iterator iter = aListSrc.iterator(); iter.hasNext() ) {<br />&nbsp;&nbsp;&nbsp; aListDest1.add(iter.next());<br />}</span></div>
<div style="margin: 0in 0in 10pt;"><span style="line-height: 115%; color: #000000; font-size: 10pt;">Like Version 1, this passes all the compile-time checks.&nbsp;And at run time, because of type erasure,&nbsp;iter.next() returns an Object and aListDest1.add consumes an Object .</span></div>
<div style="margin: 0in 0in 10pt;"><span style="line-height: 115%; color: #000000; font-size: 10pt;">But ideally we would want to be able to use the less cluttered for-each notation and&nbsp;still get rid of the castcheck.&nbsp; Can that be done?&nbsp;&nbsp;Brian Goetz's excellent article <a href="http://www.ibm.com/developerworks/java/library/j-jtp04298.html?S_TACT=105AGX02&S_CMP=EDU"><span style="color: #000000; text-decoration: none; text-underline: none;">Going Wild with Generics</span></a>&nbsp;talks about using generic methods to force the compiler to use type inference to solve a&nbsp;problem with wildcards in generics.&nbsp; To quote his article "The Java compiler doesn't perform type inference in very many places, but one place it does is in inferring the type parameter for generic methods".&nbsp; I wanted to see if the type inference from generic methods would solve our problem here and sure enough it does.</span></div>
<div style="margin: 0in 0in 10pt;"><span style="line-height: 115%; color: #000000; font-size: 10pt;">If we code up version 6 as a generic helper method</span></div>
<div style="line-height: 20.4pt; margin: 0in 0in 10pt;"><strong><em><span style="text-decoration: underline;"><span style="color: #000000; font-size: 12pt;">Version&nbsp;6</span></span></em></strong></div>
<div style="line-height: 12pt; margin: 0in 0in 10pt;"><span style="color: #000000; font-size: 10pt;">private&lt;V&gt; void splitHelper(ArrayList&lt;V&gt; src, ArrayList&lt;V&gt; dest1, ArrayList&lt;V&gt; dest2) {<br />&nbsp;&nbsp;&nbsp; for (V elem&nbsp;: src) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dest1.add(elem);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ... <br />&nbsp;&nbsp;&nbsp; }<br />}</span></div>
<div style="line-height: 20.4pt; margin: 0in 0in 10pt;"><span style="color: #000000; font-size: 10pt;">and we can then call the helper with something like</span></div>
<div style="line-height: 20.4pt; margin: 0in 0in 10pt;"><span style="color: #000000; font-size: 10pt;">&nbsp;&nbsp;&nbsp; splitHelper(aListSrc, aListDest1, aListDest2);</span></div>
<div style="margin: 0in 0in 10pt;"><span style="line-height: 115%; color: #000000; font-size: 10pt;">If we&nbsp;run version 6 thru javac and look at the generated bytecodes, we see that the checkcast bytecode that we saw in version 3 is not there, leading to better performance.</span></div>
<div style="margin: 0in 0in 10pt;"><span style="line-height: 115%; color: #000000; font-size: 10pt;">So we have found a for-each based solution that has gotten rid of the castcheck, but do others find this&nbsp;behavior surprising?&nbsp; The difference between Versions 3 and&nbsp;6 seems very minor and it seems that if the compiler could eliminate the castcheck in Version 6, it could also do so in Version 3.</span></div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<div style="margin: 0in 0in 10pt;">&nbsp;</div>
</div>]]></description>
	</item>
	
	<item>
		<dc:creator>Gary Frost</dc:creator>
		<title>Final Words</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=116433</link> 
		<pubDate>2009-07-23T12:02:06 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=116433#comments</comments>
		<trackback:ping>6</trackback:ping>
		<description><![CDATA[ <p>I have always been a little unhappy with the decision to overload the use of the 'final' keyword to enable local variables to be made available to methods in inner classes. <br />&nbsp;<br />Let's recap. Here is a method which launches a thread which prints integers 0 thru 9.<br />&nbsp;<br />public void launch(){<br />&nbsp;&nbsp; new Thread(new Runnable(){<br />&nbsp;&nbsp;&nbsp;&nbsp; public void run(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;10; i++){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("i="+i);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp; }).start();<br />}<br />&nbsp;<br />We decide to refactor this method to take two arguments (launch(int min, int max)) so that we can control the start and end values of the count.&nbsp; We might be tempted to try<br />&nbsp;<br />// will not compile<br />public void launch(int min, int max){ <br />&nbsp;&nbsp; new Thread(new Runnable(){<br />&nbsp;&nbsp;&nbsp;&nbsp; public void run(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=min; i&lt;max; i++){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("i="+i);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp; }).start();<br />}<br />&nbsp;<br />But this will fail to compile. <br />&nbsp;<br />The problem is that the parameters min and max are not in the scope of the run() method in the anonymous inner class implementation of Runnable(). In fact, because the run() method is being executed in another thread, it is likely that the original call to launch() has returned before the run() method has even started, so the variables that were on the stack when we created our Runnable() are long gone.&nbsp; To solve this problem Java needs a way to signal that a variable should be captured into the scope of any anonymous inner class that wants to use it. If Annotations were around, I suspect that an Annotation would have worked well for this, unfortunately this 'requirement' predated Annotations and it was decided to 'overload' the use of the final keyword to convey this intent.<br />&nbsp;<br />public void launch(final int min, final int max){ <br />&nbsp;&nbsp; new Thread(new Runnable(){<br />&nbsp;&nbsp;&nbsp;&nbsp; public void run(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=min; i&lt;max; i++){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("i="+i);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp; }).start();<br />}<br />&nbsp;<br />The above method will now compile and will function as suggested.<br />&nbsp;<br />But 'final' seems wrong here.&nbsp; I understand that there is a reluctance to add new key/reserved words to a language (just look at all the trouble that enum and assert created!), but final seems to be a weird choice.&nbsp; I think it breaks the law of 'least astonishment'.<br />&nbsp;<br />Let's refactor our method one more time.&nbsp; This time we will launch 10 threads per count value and we will print the 'number' of each thread. Here is our first attempt<br />&nbsp;<br />// Won't compile<br />public void launch(final int min, final int max){ <br />&nbsp;&nbsp; for (int c=0; c&lt;10; c++){<br />&nbsp;&nbsp;&nbsp;&nbsp; new Thread(new Runnable(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void run(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=min; i&lt;max; i++){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Thread "+c+" i="+i);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }).start();<br />&nbsp;&nbsp; }<br />}<br />&nbsp;<br />Again our compilation issue is that the 'c' variable is not available in the run method of the anonymous inner class. <br />We need c to be a final variable.&nbsp; Let's make it final<br />&nbsp;<br />// Won't compile for a different reason ;)<br />public void launch(final int min, final int max){ <br />&nbsp;&nbsp; for (final int c=0; c&lt;10; c++){<br />&nbsp;&nbsp;&nbsp;&nbsp; new Thread(new Runnable(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void run(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=min; i&lt;max; i++){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Thread "+c+" i="+i);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }).start();<br />&nbsp;&nbsp; }<br />}<br />&nbsp;<br />Doh! Of course c can't be final; it is a loop variable. If we mark it as 'final' we are applying the traditional (you can't mutate this) meaning of final, yet we need to mark it as final for the variable to be made available to the inner class. We are forced to do 'weird things' to get around this, like create a local final value for the purpose of capturing the value for the inner class. <br />&nbsp;<br />public void launch(final int min, final int max){ <br />&nbsp;&nbsp; for (int c=0; c&lt;10; c++){<br />&nbsp;&nbsp;&nbsp;&nbsp; final int fc = c;&nbsp;&nbsp; // fc is only used to expose a final value to the innerclass<br />&nbsp;&nbsp;&nbsp;&nbsp; new Thread(new Runnable(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void run(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=min; i&lt;max; i++){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Thread "+fc+" i="+i);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }).start();<br />&nbsp;&nbsp; }<br />}<br />&nbsp;<br />Yuck!<br />&nbsp;<br />However you might be even more surprised by this solution ;)<br />&nbsp;<br />public void launch(final int min, final int max){ <br />&nbsp;&nbsp; for (final int c: new int[]{0,1,2,3,4,5,6,7,8,9}){<br />&nbsp;&nbsp;&nbsp;&nbsp; new Thread(new Runnable(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void run(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=min; i&lt;max; i++){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Thread "+c+" i="+i);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }).start();<br />&nbsp;&nbsp; }<br />}<br />&nbsp;<br />What?<br />&nbsp;<br />So it looks like we can declare a loop variable to be final providing we are using the new for-each form.&nbsp; The code is happy to mutate it (so it's not really final, is it?) and also make it available to appropriate inner classes.&nbsp;</p>
<p>How bizarre. <br />&nbsp;<br />Next time we will look at how these final variables actually get captured/cloned into the inner classes.&nbsp; One might be surprised what is happening at the bytecode level to allow these 'final' values [to be?] made available to inner classes</p>]]></description>
	</item>
	
	<item>
		<dc:creator>Gary Frost</dc:creator>
		<title>JavaOne 2009</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=114574</link> 
		<pubDate>2009-06-11T18:30:22 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=114574#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <p>I was lucky enough to go to JavaOne last week and thought I'd share some comments, highlights, a few quibbles, and a way to make some serious money if you are in the beanbag industry. <br />&nbsp;<br />I felt that this year's JavaOne was a little subdued -- attendance seemed lower (we can probably all guess that the economy was a factor here) and generally there were fewer 'cool!' exclamations from the audiences.<br />&nbsp;<br /><strong>Monday</strong><br />&nbsp;<br />This was 'Community One' day. I attended a couple of sessions (Hadoop and Cloud related) but really spent most of the day bumping into people and catching up. CommunityOne looked a little sparsely attended at times. <br />&nbsp;<br />I did attend a session where the presenters and attendees discussed how to get the most out of their JUGs (Java User Groups).&nbsp; This was a really good session. <br />&nbsp;<br />I did enjoy hanging out in the AMD sponsored 'Hang Space,' and I had my first 'patent pending idea' here watching all of the laptop users sitting on the floor next to the walls (where the 110vac was served) and not on the comfy beanbags! So beanbag builders of the world, we need beanbags which incorporate 110v sockets.&nbsp; These could be sold in strings which connect together and will allow those slacking off at conferences to actually partake in the bean-bag offerings rather than sit on the floor.&nbsp; Of course, one might ask why the beanbags were not dragged to the walls, and the answer would be, you wouldn't be able to watch the episodes of 'The Office - US version' that were being served up on the big screen, obviously.&nbsp;&nbsp;&nbsp; I, of course, could happily sit in a beanbag, pretend to work and watch Dwight, Jim, and Pam wrestle with their plight because I have an AMD powered HP dv2 - whose battery lasted way longer than Season 1 of "The Office." <img src="i/expressions/face-icon-small-wink.gif" border="0"> <br />&nbsp;&nbsp; <br />&nbsp;<br /><strong>Tuesday<br /></strong>&nbsp;<br />It was good to see Scott McNealy handover (the keynote, not Sun just yet) to Larry Ellison. Larry's remarks regarding the importance of Java to Oracle must have made a few folks sleep easier on Tuesday evening and I suspect that the JavaFX team will be particularly pleased with Larry calling out JavaFX by name and pushing a possible OpenOffice/JavaFX integration down the line. That should be good for JavaFX and hopefully good for OpenOffice.<br />&nbsp;<br />So where is JavaFX in 2009? I count this as the third JavaOne where Sun has pushed JavaFX. 2007 was kind of a preview, and I enjoyed the demos but that was really all it was. It dominated in 2008, but was still really not cooked and I walked out of the lab session when I was asked to sign an NDA -- an NDA for a lab session at a conference that I paid to attend seemed a bit weird. Now in 2009 I really do think it might start gaining some traction.&nbsp; The addition of charting was smart (and pretty obvious really) and I was pleased that even Eclipse users got something in the form of a fairly cool Eclipse plugin.&nbsp; Now it really feels that JavaFX is not just for Netbeans anymore.&nbsp; The demos were slicker and the downloaded Eclipse plugin worked like a charm.&nbsp;&nbsp; <br />&nbsp;<br />Having worked on a large Flex application a few years back, and having seen some extremely cool Flex apps, I have always seen JavaFX as too little too late. Flash and Flex have pretty much carved up the R part of RIA (although AJAX is not dead yet!). Now I am a little more hopeful for JavaFX to at least find an audience. The more natural Java integration and the impressive binding support will appeal to those who really took to mxml+actionscript, and I can see the story developing.&nbsp; The effort that has gone into jnlp/applet deployment (on jre 1.6_10 +) has helped enormously and once we can find a way to get JavaFX to launch faster (Flash still seems to launch way faster than even trivial JavaFX apps) I think that JavaFX will come into its own.&nbsp; I look forward to kicking the tyres some more. <br />&nbsp;<br />Joshua Bloch (Google, Inc) and Neal Gafter's (Microsoft) "Return of the Puzzlers: Schlock and Awe" session was as well attended as ever. These guys do a great job presenting these infuriating corner cases. I liked the fact that they&nbsp; acknowledged making some of the mistakes presented; it makes us all feel a little less incompetent. <img src="i/expressions/face-icon-small-wink.gif" border="0"> I think I got more answers right this year, although my success rate is still not impressive.&nbsp; <br />&nbsp;<br />The "Small Language Changes in JDK(tm) Release 7" session by Joseph Darcy, Sun Microsystems, Inc. was interesting.&nbsp; I really like the 'Elvis operator' :? and also look forward to using some of the suggestions for&nbsp; less verbose 'Generic' declaration/initializations. <br />&nbsp;<br />The "Asynchronous I/O Tricks and Tips" session by Jean-Fran&ccedil;ois Arcand and Alan Bateman from Sun Microsystems, Inc. was an informative session. I really am guilty of not tracking nio (when will the 'n' in 'nio' seem really inappropriate) enough, and I look forward to using some of these tricks, especially using a 'Future' to access the response from an asynchronous read.&nbsp; <br />&nbsp;<br />One of my favourite sessions was "Toward a Renaissance VM" by Brian Goetz and John Rose from&nbsp; Sun Microsystems. Sometimes I feel my head is way too small to understand this JSR 292 of stuff, but I actually felt that I have a grasp of how this will help dynamic languages and also how it might apply to frameworks which currently rely on bytecode engines/injection and reflection to do their work.&nbsp; I still need to track down more information on this but the fog is lifting for me. <img src="i/expressions/face-icon-small-smile.gif" border="0"> <br />&nbsp;<br />I wish I had caught the "The Feel of Scala" session by Bill Venners of Artima, Inc.&nbsp; Only as the week progressed did I realize that I need to track Scala. I look forward to the slides and video of this presentation. <br />&nbsp;<br /><strong>Wednesday</strong><br />&nbsp;<br />I attended a great session called 'State: You're Doing It Wrong -- Alternative Concurrency Paradigms on the JVM&trade Machine' in the morning from Jonas Bon&eacute;r of Scalable Solutions.&nbsp; This session proposed State, Actor message passing and Data Flow mechanisms to improve concurrency.&nbsp; For me the Actor-based demos (based on Scala) not only prompted me to look at this approach in my Java apps, but also was a great example of how Scala can be scaled out.&nbsp; As I mentioned earlier I really need to dig into Scala some more. <br />&nbsp;<br />I regret missing "The Modular Java(tm) Platform and Project Jigsaw" by Mark Reinhold of Sun Microsystems, Inc. From what I have read alsewhere this modular approach is really going to help deployment and packaging. <br />&nbsp;<br />Joshua Bloch's (from Google) ""Effective Java": Still Effective After All These Years" was another opportunity to see the 'Billy Mayes' of Java (I really mean no disrespect - Josh is a pitch-perfect pitch man) do what he does flawlessly.&nbsp; His 'Effective Java' book is like the Movie 'Brazil;' you need to reread/review every year to catch what you missed previously. <br />&nbsp;<br />I enjoyed "The Ghost in the Virtual Machine: A Reference to References" session from Bob Lee, Google Inc., which went into depth regarding GC, references, and finalization issues.&nbsp; I look forward to walking through the slide deck on this one.&nbsp; I learned a lot and also know a bunch slipped on past me. <br />&nbsp;<br />I watched a cool demo which redefined classes in a running JVM using a java agent and some classloader tricks.&nbsp; This BOF session "Runtime Update of Java(tm) Technology-Based Applications, Using Dynamic Class Redefinition" by Allan Gregersen from University of Southern Denmark was fun and educational. The presenter built a Swing-based game incrementally by adding fields and methods, changing class hierarchies, etc., all without ever restarting the JVM.&nbsp; Although in practice I feel this javagent based chaining approach may not scale particularly well, if this can be pushed down into the JVM (as the presenter suggested) then this whole area has some great potential. <br />&nbsp;<br />I must apologise to my fellow AMDer, Richard West, and David Gilbert from Object Refinery Limited for missing their "JFreeChart: Surviving and Thriving" BOF.&nbsp; I look forward to picking Richard's brain about this great toolkit.<br />&nbsp;<br /><strong>Thursday<br /></strong>&nbsp;<br />Occasionally I like to see what is going on in the Swing world.&nbsp; I don't really get to write much in Swing but there are some really great toolkits out there. I particularly enjoyed "Swing Rocks: A Tribute to Filthy-Rich Clients" by Martin Gunnarsson and P&auml;r Sik&ouml; from Epsilon Information Technology. Swing really can look compelling.<br />&nbsp;<br />The "Matchmaking in the Cloud: Hadoop and EC2 at eHarmony" session from Steve Kuo and Joshua Tuberville of eHarmony, Inc. was a good presentation (and from a show of hands there were two attendees that actually got married through eHarmony so there was a cool validation of eHarmony's matching algorithm!). It walked through the technical and economic considerations around using these technologies. <br />&nbsp;&nbsp;&nbsp; <br />"Garbage Collection Tuning in the Java HotSpot(tm) Virtual Machine" from Charlie Hunt and Antonios Printezis of Sun Microsystems, Inc was a good, informative session that walked through a number of great slides highlighting what to do and what not to do.&nbsp; I still feel that GC tuning should be less of a 'dark art.'&nbsp; I worry how many JVMs are sitting out there thrashing when a few command line options would smooth the way.&nbsp; I do wish for a -XX+GCAdvise option which (possibly at the end of each GC) would suggest what command lines would be optmil with a specific workload. I know that I am supposed to use the printgc options (flag examples) to be added, and/or use visualvm to show me the graphs that I should use to determine what flags will be optimal, but this seems way too hard.&nbsp; Surely after running for a while the GC engine/subsystem would have a enough data to generate an 'I suggest running with these flags ... because ....' style report, instead of 'here are a bunch of graphs and text dumps, now go away and work out what you did wrong and come back.'&nbsp;&nbsp; Sometimes I don't want to learn to fish; sometimes I would just like to eat some fish. <img src="i/expressions/face-icon-small-wink.gif" border="0"><br />&nbsp;<br />&nbsp;<br />Cliff Click (from Azul Systems) and Brian Goetz's (Sun Microsystems)&nbsp; session,&nbsp; "This Is Not Your Father's Von Neumann Machine; How Modern Architecture Impacts Your Java(tm) Apps" was another one of the highlights of the conference.&nbsp; It was a great presentation and allowed folk without a deep understanding of microprocessor architecture to walk away with some understanding of what happens under the hood. The slide deck in the middle which walked through the issues relating to how multi-core architectures executing speculatively have to handshake over the cache was very, very slick. I am looking forward to Cliff and Brian's Boxed Set being released. <img src="i/expressions/face-icon-small-wink.gif" border="0"><br />&nbsp;<br />&nbsp;<br />There were some great sessions on&nbsp; "Actor-Based Concurrency in Scala" from Philipp Haller of EPFL and Frank Sommers of Artima which really rammed home how effective Scala and this Actor-based communication mechanism can simplify some concurrency problems.&nbsp;&nbsp; As I mentioned before this was brought up in a former session, and I enjoyed digging deeper in this dedicated session.<br />&nbsp;<br />I stayed late to enjoy the "Java(tm) Programming Language Tools in JDK(tm) Release 7" BOF on Thursday night hosted by Maurizio Cimadamore and&nbsp; Jonathan Gibbons from Sun Microsystems, Inc.&nbsp; I applaud the upcoming refactoring of javap and also enjoyed the discussion on how we might get better error reporting out of javac. I also vote [should this be "voted" in this context?] for the option of getting compilation rendered to xml to help tool chaining.&nbsp; <br />&nbsp;<br /><strong>Friday<br /></strong>&nbsp;<br />Gosling's "Toy Show" (Friday morning) did have some cool stuff; the JavaFX studio tool for composing JavaFX without coding does look very, very good. Also the image analysis toolkit which generated analytical 'hashes' for images and then allowed image related searching/matching was very impressive. My favourite was the Printer/Copier based Java app for creating arbitrary multiple choice exam papers or surveys on plain paper, then printing a bunch of the question papers off and by feeding a special page with the answers and the response papers into the scanner, allow the copier/printer to grade the papers. <img src="i/expressions/face-icon-small-wink.gif" border="0">&nbsp; Very smart.&nbsp; <br />&nbsp;<br />The "Under the Hood: Inside a High-Performance JVM(tm) Machine" session from Trent Gray-Donald of IBM was excellent.&nbsp; This provided some more insight into what happens when your code is executed by a modern JVM. <br />&nbsp;<br />Sadly I missed afternoon sessions because I had to get to the airport to get home to watch season two of 'The Office.' <img src="i/expressions/face-icon-small-wink.gif" border="0"><br />&nbsp;<br />There certainly is enough to dig into to keep me busy enough until next year. <br />&nbsp;</p>
<p>&nbsp;</p>]]></description>
	</item>
	
	<item>
		<dc:creator>Ben Pollan</dc:creator>
		<title>How Complex Is Your JRE Command-line?</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=114556</link> 
		<pubDate>2009-06-11T12:04:41 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=114556#comments</comments>
		<trackback:ping>1</trackback:ping>
		<description><![CDATA[ <p>Guess how many command-line flags there are for the server JRE in the OpenJDK?&nbsp; I'm hearing 42.&nbsp; Kudos to all of you fans of the late Douglas Adams, but you're slightly short of the real answer.&nbsp; It's 477 (give or take a flag or two).&nbsp; To confirm, just go into src\share\vm\runtime\globals.hpp and src\share\vm\opto\c2_gloabls.hpp, which define them.&nbsp; The flags control all sorts of things, some of which you are probably very familiar with like the heap (-Xms -Xmx), and some which you may not know about, such as the memory footprint settings (-XX:ReservedCodeCacheSize and -XX:InitialCodeCacheSize).</p>
<p>I'm not asking you this because I want to know if you have intimate knowledge of the JRE (although if you can keep bits of trivia like this in your head, I am truly impressed).&nbsp; My question really comes out of the world of performance analysis of Java runtimes.&nbsp; Suffice it to say that as the Java Labs works to improve JRE performance, sometimes our analysis leads to improvements that can be realized by tuning these existing command-line flags.&nbsp; But here's my theory...I bet most of you use few, if any, of these flags in production.&nbsp; You probably have very good reasons for doing this.&nbsp; You may not have access to the command line, or you may have different applications, some of which my benefit from certain flags, while others won't.&nbsp; If true, the result is the same...when we look to improve JRE performance, we really need to do it in a way that is engineered to help potentially any application in a flexible way that does not require changes to the command line.</p>
<p>So answer these two questions:</p>
<ul>
<li>Do you set any command line flags in production?</li>
<li>If yes, what are they?</li>
</ul>]]></description>
	</item>
	
	<item>
		<dc:creator>Tom Deneau</dc:creator>
		<title>A Java Generics Performance Puzzler</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=114296</link> 
		<pubDate>2009-06-05T15:41:31 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=114296#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span style="font-size: larger;"><span style="color: #000000;">In this entry, we&rsquo;ll go down that well-worn path of looking at some microbenchmark results and trying to explain them.</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span style="font-size: larger;"><span style="color: #000000;">This microbenchmark created an ArrayList such that if one went thru the ArrayList in order, the entries were randomly distributed in memory.&nbsp;We also had enough elements in the list that it would take some time to go thru the list.&nbsp;We then wanted to go thru the list in order and &ldquo;split&rdquo; it so that we created two new ArrayLists, one for all the even elements and one for all the odd elements.</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span style="font-size: larger;"><span style="color: #000000;">There are a number of ways to code the splitting but let&rsquo;s start with an approach that doesn&rsquo;t use Iterators, but just uses an integer index to the get method for the source and then adds (appends) to the destination ArrayList.&nbsp;The body of the loop might then look like the following:</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><strong><em><span style="text-decoration: underline;"><span style="color: #000000; font-size: 12pt;">Version&nbsp;1</span></span></em></strong></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span style="color: #000000; font-size: 10pt;">&nbsp;&nbsp;ArrayList aListSrc, aListDest1, aListDest2;<br /></span>&nbsp;<span style="color: #000000; font-size: 10pt;">&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt;"> ...<br />&nbsp;&nbsp; while (idxSrc &lt; NUMOBJS) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aListDest1.add(aListSrc.get(idxSrc));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aListDest2.add(aListSrc.get(idxSrc+1));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; idxSrc+=2;<br />&nbsp;&nbsp; }<br /></span><span style="color: #000000; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">After measuring version 1, you decide those add method lines are a bit wordy so you break them into two statements, using a local variable to hold the intermediate result.&nbsp;Or perhaps you wanted to print some debug information for each element as you are copying it, and you needed a local variable to hold the element reference (and you then removed the debug statements).&nbsp;So you end up with something like:</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><strong><em><span style="text-decoration: underline;"><span style="color: #000000; font-size: 12pt;">Version&nbsp;2</span></span></em></strong><span style="color: #000000; font-size: 12pt;"><br /></span><span style="color: #000000; font-size: 10pt;">&nbsp;&nbsp;ArrayList aListSrc, aListDest1, aListDest2;<br /></span>&nbsp;<span style="color: #000000; font-size: 10pt;">&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt;"> ...<br />&nbsp;&nbsp; while (idxSrc &lt; NUMOBJS) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyClass myc = aListSrc.get(idxSrc);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aListDest1.add(myc);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myc = aListSrc.get(idxSrc+1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aListDest2.add(myc);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;idxSrc+=2;<br />&nbsp;&nbsp; }<br /><br /></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">But when you measure version 2, you find that it is much slower than version 1 (about 1/3 the speed in my measurements).&nbsp;Before reading on, you might try to figure out why.&nbsp;Is the JVM perhaps not able to optimize away the store to the local variable?&nbsp;And if so, is the store to the local variable&nbsp;really that expensive?&nbsp; I will add that in both cases, the get and add methods got inlined nicely into the timed loop.</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><strong><span style="text-decoration: underline;"><span style="color: #000000; font-size: small;">Answer</span></span></strong></span><strong></strong></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">You may recall that generics in Java are implemented with type checking at compile time but with type erasure at run time.&nbsp;How does that impact us?&nbsp;Well for one it means that at runtime the call to </span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aListSrc.get(idxSrc);</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">really returns an Object, even though aListSrc is an ArrayList.&nbsp;Therefore the statement from version 2:</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyClass myc = aListSrc.get(idxSrc);</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">requires a runtime castcheck that the Object returned by aListSrc really is a MyClass.&nbsp;If you look at the byte codes generated for such a statement, you will see a checkcast bytecode.</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">To&nbsp;check&nbsp;whether the object returned by aList.get really is of type MyClass (or a child of MyClass) the JVM must read the header of the object.&nbsp;In our list splitting operation however we never had any other reason to look at any of the fields of the MyClass objects as we went thru the list.&nbsp;We just copied each MyClass reference from the source list into one of the destination lists.&nbsp;So by having to look at the header as part of the castcheck, we must now wait until&nbsp;the object is read from memory into the processor&rsquo;s cache.&nbsp;And with lots of objects in the list, it makes it less likely that an object is already in the cache when we need it.</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">How did we avoid the castcheck in Version 1?&nbsp;&nbsp; In version 1, the javac compiler used the list&rsquo;s type declaration ArrayList to guarantee that the returned object was of type MyClass at compile time.&nbsp;&nbsp; And at runtime the types from the generics&nbsp;were erased so basically we have&nbsp;a get method returning an object which is passed to an add method which&nbsp;takes an Object.&nbsp;&nbsp;So no checkcast is necessary.</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">Note that we can try to get around the checkcast&nbsp;&nbsp;by just declaring the local variable to be an Object rather than a MyClass, but now the javac compiler will rightly complain when we try to do an add&nbsp;of an Object into an ArrayList.</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><strong><em><span style="text-decoration: underline;"><span style="color: #000000; font-size: 12pt;">Version 3 (will not compile)<br /></span></span></em></strong><span style="color: #000000; font-size: 10pt;">&nbsp;&nbsp;ArrayList aListSrc, aListDest1, aListDest2;<br /></span>&nbsp;<span style="color: #000000; font-size: 10pt;">&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt;"> ...<br />&nbsp;&nbsp; while (idxSrc &lt; NUMOBJS) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>Object</strong> myc = aListSrc.get(idxSrc);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aListDest1.add(myc);&nbsp;&nbsp; // error<br /></span><span style="color: #000000; font-size: 10pt;">&nbsp;&nbsp; </span>&nbsp;<span style="color: #000000; font-size: 10pt;">&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt;"> ...<br />&nbsp;&nbsp; }<br /><br /></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">I should note here that if our original algorithm had looked at fields of the MyClass objects to make some decision on how to split the list, then the object would have already have to be read from memory for the other field accesses and the extra time to do the header check for the castcheck would have been insignificant.</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">Even though the above is explainable by type erasure, I&rsquo;m not sure it follows the principal of least surprise.&nbsp;After all, I declared aListSrc to be ArrayList and all I did was assign the .get output to a MyClass object.&nbsp;If the javac compiler knew enough to eliminate the castcheck between the output of the get and the input of the add, why couldn&rsquo;t it eliminate it between the output of the get and the&nbsp;assignment to the local variable?</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">Looking at this from another angle, one might ask whether the JVM can&nbsp;optimize away&nbsp;the castcheck at runtime.&nbsp;A check with the Hotspot folks indicated that the bytecodes are saying "throw an exception if aListSrc.get ever returns a non-MyClass object".&nbsp;&nbsp;And the JVM cannot elide bytecodes that <em>could </em>cause an exception like this.</span></span></div>
<div style="line-height: normal; margin: 0in 0in 10pt; vertical-align: top;"><span><span style="color: #000000; font-size: small;">So the message is don't cast your return from the Collections classes like this&nbsp;if you don't need to.</span></span></div>
</div>]]></description>
	</item>
	
	<item>
		<dc:creator>Ben Pollan</dc:creator>
		<title>New Virtualization Article</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=113324</link> 
		<pubDate>2009-05-15T11:28:14 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=113324#comments</comments>
		<trackback:ping>1</trackback:ping>
		<description><![CDATA[ <p>Check out&nbsp;this new article in the Java Zone: <a href="http://developer.amd.com/documentation/articles/pages/OptimizingJavaInVMEnvironment.aspx">Optimizing Java Performance in a Virtualized Environment</a>.&nbsp; It's based on a JavaOne 2008 Tech Session of the same name by Shrinivas and Azeem, which provided a good overview of how to navigate the intersecting worlds of Java and Virtualization.</p>
<p>Let us know what you think.</p>]]></description>
	</item>
	
	<item>
		<dc:creator>Gary Frost</dc:creator>
		<title>Putting Enums to work</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=112715</link> 
		<pubDate>2009-05-01T12:42:21 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=112715#comments</comments>
		<trackback:ping>1</trackback:ping>
		<description><![CDATA[ <p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">Most Java developers are probably aware that enums were added to Java 1.5 and we are becoming more familiar with seeing them used like this: </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 12pt; LINE-HEIGHT: 20.4pt"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">enum LIGHT { RED, AMBER, GREEN};</span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;<br />Here we&nbsp; are defining an enum that we can use to hold the state of a traffic light. The above code allows LIGHT to be used as a new type. <br /></span><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">LIGHT light = LIGHT.RED;</span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;"><br />and via some magic we can use&nbsp;LIGHT values&nbsp;in switch constructs. <br /></span><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">switch(light){</span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';"><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; case RED: </span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Stop");</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; case AMBER: </span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Get ready");</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; case GREEN: </span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Go");</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;</span></strong><br /><strong><span style="font-family: 'Courier New';">}</span></strong></span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;"><br />we can iterate over the values of </span><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">LIGHT </span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">using the array returned from the </span><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">values()</span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;"> accessor..<br />&nbsp;<br /></span><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">for (LIGHT light:LIGHT.values()){</span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';"><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp; System.out.println(light);</span></strong><br /><strong><span style="font-family: 'Courier New';">}</span></strong></span></strong></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">and can also&nbsp;perform&nbsp;ordinal comparisons..<br /></span><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">if (light &lt; LIGHT.GREEN.ordinal()){</span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';"><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; System.out.println("Not yet!");</span></strong><br /><strong><span style="font-family: 'Courier New';">}</span></strong></span></strong></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt">&nbsp;</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">Because enums are indeed Classes we can customize them by adding fields, constructors and methods.</span><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">So if we wanted to be able&nbsp;to query&nbsp;each 'value' for the next in the sequence (including wrapping from GREEN to RED) we can use&nbsp;&nbsp;:-</span><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">current.values()[(current.ordinal()+1)%current.values().length]</span></strong></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt">&nbsp;</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">However, rather than having this logic spill out into the code using the enum, we can provide a method in the enum itself</span><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">enum LIGHT {</span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';"><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; RED, AMBER, GREEN;</span></strong></span></strong></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">&nbsp;&nbsp; LIGHT next(){</span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';"><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(values()[(this.ordinal()+1)%values().length]);</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; </span></strong><br /><strong><span style="font-family: 'Courier New';">}</span></strong></span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">So now we can query the next value using</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt">&nbsp;</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">light = light.next();</span></strong></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt">&nbsp;</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">We can also overload methods for each value. So an alternative to the above implementation might be</span><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">enum LIGHT {</span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';"><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; RED,</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; AMBER{</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LIGHT next(){</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(GREEN);</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; },</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; GREEN{</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LIGHT next(){</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(RED);</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; },</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; // We need a method to override, so lets assume RED is the default</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; LIGHT next(){</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(AMBER);</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; </span></strong><br /><strong><span style="font-family: 'Courier New';">}</span></strong></span></strong></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt">&nbsp;</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">Which is a little more verbose, but in some ways more explicit.&nbsp; Note that we must provide an implementation for the enum and then each 'value' can overload this if it chooses. </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt">&nbsp;</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">Although we can't extend enums (and probably for good reason) we can implement interfaces.</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt">&nbsp;</p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">Let&rsquo;s say we had an application which deals with a bound set of file types (XML, TEXT and ANY). We could make a FILE_TYPE enum which supports the FileFilter interface. </span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">enum FILE_TYPE implements FileFilter&nbsp; {</span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';"><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; ANY,</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; TXT{</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean accepts(File _file){</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(_file.getName().endsWith(".txt") || _file.getName().endsWith(".text"));</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String getDescription(){</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return("TXT files");</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; }</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; XML{</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean accepts(File _file){</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(_file.getName().endsWith(".xml"));</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String getDescription(){</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return("XML files");</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; }</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; boolean accepts(File _file){</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(true);</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; }</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; String getDescription(){</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return("Any file");</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp; }</span></strong><br /><strong><span style="font-family: 'Courier New';">}</span></strong></span></strong></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">This then allows us to write a method for getting a file from a JFileChooser dialog...</span><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">File getFile(FILE_TYPE _fileType){</span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';"><br /><strong><span style="font-family: 'Courier New';">&nbsp;JFileChooser chooser = new JFileChooser();</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;filter.setDescription(_fileType.getDescription());</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;chooser.setFileFilter(_fileType);</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;int returnVal = chooser.showOpenDialog(parent);</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;if(returnVal == JFileChooser.APPROVE_OPTION) {</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;&nbsp;&nbsp; return(chooser.getSelectedFile());</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;}</span></strong><br /><strong><span style="font-family: 'Courier New';">&nbsp;return(null);</span></strong><br /><strong><span style="font-family: 'Courier New';">}</span></strong></span></strong></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">So we can ask for an XML file using..</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: 'Courier New';">File file = getFile(FILE_TYPE.XML);</span></strong></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: 20.4pt"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">Obviously we need to be careful and not 'misuse' them, but&nbsp;I believe that enums can offer options beyond the traditional static list of values.</span></p>]]></description>
	</item>
	
	<item>
		<dc:creator>Ben Pollan</dc:creator>
		<title>Java Posse interview</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=111981</link> 
		<pubDate>2009-04-15T11:41:42 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=111981#comments</comments>
		<trackback:ping>2</trackback:ping>
		<description><![CDATA[ <p>A few weeks ago, the Java Posse interviewed Azeem, Gary, and I.&nbsp; The <a href="http://javaposse.com/index.php?post_id=454436">podcast</a> has been posted!&nbsp; A lot of great topics were covered, including JVM performance, multi-core programming, developer tools and more.&nbsp; Have a listen, then comment here.</p>
<p>Many thanks to the Java Posse for the opportunity.</p>
<p>Ben</p>]]></description>
	</item>
	
	<item>
		<dc:creator>Tom Deneau</dc:creator>
		<title>Using Apache JMeter in non-GUI mode</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=111320</link> 
		<pubDate>2009-03-31T18:23:40 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=111320#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <p>Many of us are&nbsp;familiar with <a href="http://jakarta.apache.org/jmeter/">Apache's JMeter tool</a>, an open source tool which can help load test and measure the performance of web applications.&nbsp; JMeter has an excellent GUI mode and this is the mode that is presented if you invoke JMeter with no arguments. &nbsp;During script development, this GUI mode is the way to go.&nbsp; New configuration elements, thread groups and samplers can be added and edited and the results from runs can be viewed with a number of different listeners which helps with debugging.</p>
<p>When the script is complete and debugged, however, you may find it more convenient to run JMeter in non-GUI mode.&nbsp; Because of the reduced overhead, you may also find you can&nbsp;drive more&nbsp;requests per second out of JMeter in non-GUI mode. &nbsp;Running a finished test plan in non-GUI mode is fairly simple, use the -n option for non-GUI mode and the -t option to specify the test plan as follows:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JMeter -n -t MyTestPlan.jmx</p>
<p>However, if you take an existing test plan you developed in GUI mode and run it as above, chances are the only output you'll see will be something like the following:</p>
<p>Created the tree successfully using MyTestPlan.jmx<br />Starting the test @ Mon Mar 02 14:01:13 CST 2009 (1236024073938)<br />Tidying up ...&nbsp;&nbsp;&nbsp; @ Mon Mar 02 14:01:15 CST 2009 (1236024075844)<br />... end of run</p>
<p>with no additional information sent to stdout.&nbsp; Any listeners you happened to have in your test plan were probably GUI-mode-specific and don't send anything to non-GUI mode.&nbsp; So how do we see how we're doing regarding throughput and/or response time?</p>
<p><strong>Using the JMeter Summariser</strong><strong></strong></p>
<p>The Summariser is like a special listener that only applies to non-GUI mode.&nbsp;&nbsp;&nbsp; It is controlled through JMeter properties.&nbsp; JMeter properties can be set either by</p>
<ul class="unIndentedList">
<li>editing the default in the bin/JMeter.properties file</li>
<li>creating a new properties file and specifycing that on the JMeter command line (-p option)</li>
<li>specifying any number of individual properties on the JMeter command line using the -J or -D options (type JMeter -help to see the command line options).</li>
</ul>
<p>The following properties affect the summariser:</p>
<p># Define the following property to automatically start a summariser<br /># with that name(applies to non-GUI mode ony)<br />summariser.name=summary<br />#<br /># interval between summaries (in seconds) default 3 minutes<br />summariser.interval=180<br />#<br /># Write messages to log file<br />summariser.log=true<br />#<br /># Write messages to System.out<br />summariser.out=true</p>
<p>So with the above properties enabled, we would get a summary sent every 180 seconds to both System.out and the log file.&nbsp; Note the summariser.name allows other summarisers to be developed and plugged in but I have not investigated that yet.&nbsp; The default summariser has been pretty useful.&nbsp; Here's an example of what you get from the default summariser:</p>
<p>summary +&nbsp;&nbsp;&nbsp; 41 in&nbsp; 15.4s =&nbsp;&nbsp;&nbsp; 2.7/s Avg:&nbsp; 2234 Min:&nbsp;&nbsp; 383 Max:&nbsp; 6974 Err:&nbsp;&nbsp;&nbsp;&nbsp; 0 (0.00%)</p>
<p>summary +&nbsp;&nbsp;&nbsp; 57 in&nbsp; 21.5s =&nbsp;&nbsp;&nbsp; 2.6/s Avg:&nbsp; 2548 Min:&nbsp;&nbsp; 618 Max:&nbsp; 4528 Err:&nbsp;&nbsp;&nbsp;&nbsp; 0 (0.00%)</p>
<p>summary =&nbsp;&nbsp;&nbsp; 98 in&nbsp; 32.5s =&nbsp;&nbsp;&nbsp; 3.0/s Avg:&nbsp; 2416 Min:&nbsp;&nbsp; 383 Max:&nbsp; 6974 Err:&nbsp;&nbsp;&nbsp;&nbsp; 0 (0.00%)</p>
<p>summary +&nbsp;&nbsp; 108 in&nbsp; 21.8s =&nbsp;&nbsp;&nbsp; 5.0/s Avg:&nbsp; 1291 Min:&nbsp;&nbsp; 229 Max:&nbsp; 6317 Err:&nbsp;&nbsp;&nbsp;&nbsp; 0 (0.00%)</p>
<p>summary =&nbsp;&nbsp; 206 in&nbsp; 52.5s =&nbsp;&nbsp;&nbsp; 3.9/s Avg:&nbsp; 1827 Min:&nbsp;&nbsp; 229 Max:&nbsp; 6974 Err:&nbsp;&nbsp;&nbsp;&nbsp; 0 (0.00%)</p>
<p>&nbsp;</p>
<p>The lines with "summary +" are incremental for the latest summariser period, the lines with "summary =" are cumulative.&nbsp; The above was with a summariser period of 20 secs, you can see the actual periods can sometimes be longer than the specified period and the length of the very first period is somewhat random.&nbsp; You get the throughput statistics as well as average, min and max response times, and how many errors were detected (assuming your JMeter test plan as assertions to detect errors).</p>
<p><strong>The JMeter log file</strong><strong></strong></p>
<p>You may have noticed that one of the summariser properties was whether we wanted to send summariser output to the log file.&nbsp; Every JMeter run produces a log file, by default bin/JMeter.log.&nbsp;&nbsp; In either GUI or non-GUI mode, you can specify a different log file directly on the command line using the -j option.&nbsp; And as with most things in JMeter, the actual name of the log file is configurable through a property, in this case the log_file property.&nbsp; One interesting option allows the log_file property to include the date, such as</p>
<p>log_file='JMeter_'yyyyMMddHHmmss'.tmp'</p>
<p>So when you enable summariser.log=true as we did above, the summary lines we saw on stdout will also appear in the log file, with an identifying header such as "INFO&nbsp; - JMeter.reporters.Summariser:" to show their source (they will be mixed with other JMeter log information).</p>
<p>&nbsp;</p>
<p><strong>Logging samples in non-GUI mode</strong></p>
<p>Note that the JMeter usage message&nbsp;refers to a different kind of log file which can be used to log the actual samples in an xml file format called JTL:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -l, --logfile &lt;argument&gt;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; the file to log samples to</p>
<p>&nbsp;</p>
<p>So in non-GUI mode this can be used to log the actual sample information much as the View Results Tree listener does in GUI mode.&nbsp; In fact, you can take the jtl file produced here and load it into the listeners in GUI mode to get a more readable display.&nbsp; Because of the intrusive overhead of logging all samples, you probably won't want to do that in measurement mode but you might do it when debugging a problem.</p>
<p>In a future post, we'll get into a couple of other useful JMeter topics:</p>
<ul type="disc">
<li>Using the scripting configuration elements supported by JMeter to add dynamic run time information to the log file.</li>
<li>Having finer control over what gets saved in the .jtl file</li>
</ul>
<p>Meanwhile, I'd be interested in learning how others use JMeter in non-GUI mode.</p>
<p>&nbsp;</p>]]></description>
	</item>
	
	<item>
		<dc:creator>Azeem Jiva</dc:creator>
		<title>Accurately profiling code with Instruction Based Sampling</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=111115</link> 
		<pubDate>2009-03-27T09:36:43 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=111115#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <p class="MsoNormal" style="line-height: 20.4pt; margin: 0in 0in 0pt;"><span style="font-size: small;"><span style="font-family: "Calibri","sans-serif"; color: #4c4c4c;">Most modern processors are </span><span style="font-family: "Verdana","sans-serif"; color: #4c4c4c; font-size: 8pt;"><a title="Superscalar" href="http://en.wikipedia.org/wiki/Superscalar" target="_blank"><span style="font-family: "Calibri","sans-serif"; color: #007878; font-size: 12pt; text-decoration: none; text-underline: none;">superscalar</span></a></span></span><span style="font-family: "Calibri","sans-serif"; color: #4c4c4c;"><span style="font-size: small;"> or implement a form of parallism where instructions can schedule and execute multiple instructions at one time.&nbsp; The latest AMD Opteron&trade; processors are superscalar which allows them to run code faster than would otherwise be possible at the same clock rate.&nbsp; This can present a few problems with respect to finding performance bottlenecks.&nbsp; Since the latest AMD Opteron&trade; processors can&nbsp;execute and track&nbsp;up to 72 instructions in flight at one time, it becomes difficult to properly match up source code to the in-flight instructions.&nbsp;&nbsp;<br />&nbsp;<br />What does all of the above have to do with profiling your application?&nbsp; If you are investigating performance issues with a timer based profile then you are more interested in how many cycles a method took to execute.&nbsp; And a few instructions being mapped to the wrong method is not of much consequence.&nbsp; Although if you are running profiles with performance counters then the lack of precision between when an instruction performed the event you are tracking and when that value shows up in the counter could throw off your analysis.&nbsp; Traditional profiling&nbsp;tools will not be able to accurately match the performance counter timer samples with the generated X86 assembly code.&nbsp; Instructions in flight can retire at any time, depending on memory access, hits in cache, stalls in the pipeline and many other factors and performance counter events can be attributed to the wrong instruction.&nbsp; This is even more difficult in a managed environment like Java where the generated code is dynamically created and executed.&nbsp;&nbsp;</span></span><span style="font-family: "Verdana","sans-serif"; color: #4c4c4c; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="line-height: 20.4pt; margin: 0in 0in 0pt;"><span style="font-family: "Verdana","sans-serif"; color: #4c4c4c; font-size: 8pt;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: 20.4pt; margin: 0in 0in 0pt;"><span style="font-size: small;"><span style="font-family: "Calibri","sans-serif"; color: #4c4c4c;">All of this adds up to inaccurate mappings between X86 assembly and performance counters which can mislead most performance engineers into&nbsp;fixing performance bottlenecks on the wrong sequence of&nbsp;code.&nbsp; </span></span></p>
<p class="MsoNormal" style="line-height: 20.4pt; margin: 0in 0in 0pt;"><span style="font-family: "Verdana","sans-serif"; color: #4c4c4c; font-size: 8pt;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: 20.4pt; margin: 0in 0in 0pt;"><span style="font-size: small;"><span style="font-family: "Calibri","sans-serif"; color: #4c4c4c;">One way around this is to use better implementations of hardware performance counters.&nbsp; The latest AMD Opteron&trade; processors have something that can help.&nbsp; Instruction Based Sampling (IBS) provides precise information about the execution of instructions.&nbsp; IBS provides four advantages over conventional performance counters:</span></span></p>
<p class="MsoNormal" style="line-height: 20.4pt; margin: 0in 0in 0pt;"><span style="font-family: "Verdana","sans-serif"; color: #4c4c4c; font-size: 8pt;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: 20.4pt; text-indent: -0.25in; margin: 0in 0in 0pt 0.5in; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level1 lfo1; tab-stops: list .5in;"><span style="font-family: "Verdana","sans-serif"; color: #4c4c4c; font-size: 8pt; mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana;"><span style="mso-list: Ignore;">1.<span style="font-family: "Times New Roman";">&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-size: small;"><span style="font-family: "Calibri","sans-serif"; color: #4c4c4c;">Hardware events are attributed precisely to the instruction that caused the event.</span></span></p>
<p class="MsoNormal" style="line-height: 20.4pt; text-indent: -0.25in; margin: 0in 0in 0pt 0.5in; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level1 lfo1; tab-stops: list .5in;"><span style="font-family: "Verdana","sans-serif"; color: #4c4c4c; font-size: 8pt; mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana;"><span style="mso-list: Ignore;">2.<span style="font-family: "Times New Roman";">&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-size: small;"><span style="font-family: "Calibri","sans-serif"; color: #4c4c4c;">A wide range of events are gathered, and are not limited to four out of many events that must be specified at the beginning of the profile.&nbsp;&nbsp;</span></span></p>
<p class="MsoNormal" style="line-height: 20.4pt; text-indent: -0.25in; margin: 0in 0in 0pt 0.5in; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level1 lfo1; tab-stops: list .5in;"><span style="font-family: "Verdana","sans-serif"; color: #4c4c4c; font-size: 8pt; mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana;"><span style="mso-list: Ignore;">3.<span style="font-family: "Times New Roman";">&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-size: small;"><span style="font-family: "Calibri","sans-serif"; color: #4c4c4c;">&nbsp;Virtual and physical addresses of load/store operands are collected.&nbsp; This allows managed environments to associate specific data structures with X86 instructions.</span></span></p>
<p class="MsoNormal" style="line-height: 20.4pt; text-indent: -0.25in; margin: 0in 0in 0pt 0.5in; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level1 lfo1; tab-stops: list .5in;"><span style="font-family: "Verdana","sans-serif"; color: #4c4c4c; font-size: 8pt; mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana;"><span style="mso-list: Ignore;">4.<span style="font-family: "Times New Roman";">&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-size: small;"><span style="font-family: "Calibri","sans-serif"; color: #4c4c4c;">Latency is measured for key performance parameters such as data cache miss.</span></span></p>
<p class="MsoNormal" style="line-height: 20.4pt; margin: 0in 0in 0pt;"><span style="font-family: "Verdana","sans-serif"; color: #4c4c4c; font-size: 8pt;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: 20.4pt; margin: 0in 0in 0pt;"><span style="font-family: "Calibri","sans-serif"; color: #4c4c4c; font-size: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">For more information about IBS, see the </span><span style="font-family: "Verdana","sans-serif"; color: #4c4c4c; font-size: 8pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"><a title="AMD Software Optimization guide" href="http://developer.amd.com/documentation/guides/Pages/default.aspx" target="_blank"><span style="font-family: "Calibri","sans-serif"; color: #007878; font-size: 12pt; text-decoration: none; text-underline: none;">AMD Software Optimization guide</span></a></span><span style="font-family: "Calibri","sans-serif"; color: #4c4c4c; font-size: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"> for </span><span style="font-family: "Verdana","sans-serif"; color: #4c4c4c; font-size: 8pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"><a title="AMD Family 10h Processors" href="http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/40546.pdf" target="_blank"><span style="font-family: "Calibri","sans-serif"; color: #007878; font-size: 12pt; text-decoration: none; text-underline: none;">AMD Family 10h Processors</span></a></span><span style="font-family: "Calibri","sans-serif"; color: #4c4c4c; font-size: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">.</span></p>]]></description>
	</item>
	
	<item>
		<dc:creator>Ben Pollan</dc:creator>
		<title>Java Labs interview by Java Posse soon</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=110907</link> 
		<pubDate>2009-03-23T11:20:31 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=110907#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <div>I'm happy to announce that the Java Labs will be interviewed by the <a href="http://www.javaposse.com/">Java Posse</a>&nbsp;tomorrow.&nbsp; Their weekly podcast keeps the development community up-to-speed on the world of Java.&nbsp; While we certainly plan to discuss the involvement of the team and AMD in that world, the interview gives us the opportunity to discuss the points that are important to you.</div>
<div>&nbsp;</div>
<div>Do you have a pressing topic that you want us to weave into the discussion?&nbsp; Let us know by commenting here.</div>
<div>&nbsp;</div>
<div>Thanks,</div>
<div>Ben</div>
<div>&nbsp;</div>]]></description>
	</item>
	
	<item>
		<dc:creator>Shrinivas Joshi</dc:creator>
		<title>Java Object Trimming</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=110221</link> 
		<pubDate>2009-03-10T11:22:33 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=110221#comments</comments>
		<trackback:ping>5</trackback:ping>
		<description><![CDATA[ <p><!--[if gte mso 9]><xml> Normal   0         false   false   false                             MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><xml> </xml><![endif]-->
<style>

</style>
<!--[if gte mso 10]>
<style>
 /* Style Definitions */
 table.MsoNormalTable
	{mso-style-name:"Table Normal";
	mso-tstyle-rowband-size:0;
	mso-tstyle-colband-size:0;
	mso-style-noshow:yes;
	mso-style-parent:"";
	mso-padding-alt:0in 5.4pt 0in 5.4pt;
	mso-para-margin:0in;
	mso-para-margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:10.0pt;
	font-family:"Times New Roman";
	mso-ansi-language:#0400;
	mso-fareast-language:#0400;
	mso-bidi-language:#0400;}
</style>
<![endif]--></p>
<p>Things like code refactorings,&nbsp;insufficient code coverage testing, poor coding standards or mere oversight can often lead to redundant code. Generally all commercial JVMs apply a&nbsp;certain level of escape analysis and dead code elimination optimizations&nbsp;while executing Java code. However, there are certain cases where JVM&nbsp;is unable&nbsp;to eliminate redundancy; for example, unused instance variables of a class.</p>
<p>&nbsp;</p>
<p>IDEs such as Eclipse can warn the&nbsp;Developers about local variables and instance variables not being read by the program. It might be easier for JVMs to identify and optimize away redundant local variables. However, it would not be possible for a JVM to eliminate unused instance variables that can potentially be accessed using mechanisms such as Java Reflection. These unused instance variables will increase object size and, in turn, the memory footprint of the Java application if a large number of such objects are allocated by the program.</p>
<p>&nbsp;</p>
<p>You might think that such unused/unread instance variables might not have a big impact on the application performance. Contrary to this belief, such redundant variables can substantially affect application performance. In fact, this could lead to pathological cases where application performance can be hampered due to inefficient processor memory cache usage. For example, a redundant instance variable which gets mapped to the end of object layout in the memory&nbsp;can increase the object size such that the resulting object&nbsp;does not fit into a single cache line. On the other hand, when such a field&nbsp;gets mapped into middle&nbsp;parts of the object layout, it can lead to memory holes and result in redundant garbage&nbsp;collection cost.</p>
<p>&nbsp;</p>
<p>The following benchmark demonstrates performance effects of unused instance variables:</p>
<p><strong>&nbsp;</strong></p>
<p><strong>import</strong> java.util.LinkedList;</p>
<p>&nbsp;</p>
<p><strong>public</strong> <strong>final</strong> <strong>class</strong> ObjectTrimming {</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>private</strong> <strong>long</strong> <span style="text-decoration: underline;">redundantField1</span> = 0L;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>private</strong> <strong>long</strong> <span style="text-decoration: underline;">redundantField2</span> = 0L;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>private</strong> <strong>long</strong> <span style="text-decoration: underline;">redundantField3</span> = 0L;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>private</strong> <strong>long</strong> <span style="text-decoration: underline;">redundantField4</span> = 0L;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>private</strong> <strong>long</strong> <span style="text-decoration: underline;">redundantField5</span> = 0L;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>private</strong> <strong>long</strong> <span style="text-decoration: underline;">redundantField6</span> = 0L;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>private</strong> <strong>long</strong> <span style="text-decoration: underline;">redundantField7</span> = 0L;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>private</strong> <strong>byte</strong>[] <span style="text-decoration: underline;">usedField</span> = <strong>null</strong>;</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>private</strong> ObjectTrimming(){</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; usedField = <strong>new</strong> <strong>byte</strong>[968];</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>static</strong> <strong>void</strong> main(String[] args) <strong>throws</strong> Exception {</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LinkedList listStore = <strong>new</strong> LinkedList();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>long</strong> totalTime = 0L;</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>for</strong>(<strong>int</strong> j=0; j&lt;10;j++)</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>long</strong> then = System.<em>currentTimeMillis</em>();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>for</strong>(<strong>int</strong> i=0; i&lt; 104857; i++)</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listStore.addLast(<strong>new</strong> ObjectTrimming());</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>long</strong> now = System.<em>currentTimeMillis</em>();&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; totalTime += now - then;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<em>out</em>.println("After Iteration " +j + " total number of elements in the Linked List: " +listStore.size());</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<em>out</em>.println("Execution Time: " + totalTime);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>}</p>
<p>&nbsp;</p>
<p>As you can see, this application creates approximately 1024M of data. The ObjectTrimming class has 7 fields of <em>long</em> data type and a <em>byte</em> array. Since each <em>long</em> field occupies 64 bits of data, objects of type ObjectTrimming class will have 56 bytes of space occupied by <em>long</em> fields. We are using the byte array to allocate another 968 bytes of data which will bring each ObjectTrimming object to a size of 1KB. We are creating 104857 such objects in a loop which iterates for 10 times. Thus in the end, <em>listStore</em> linked list will contain approximately 1024M of data.</p>
<p>&nbsp;</p>
<p>We executed this benchmark three times on a system with configuration of 2 chips, 8 CPUs, 4 cores per chip AMD Opteron 8384 processor running at 2.7GHz, with 8G of RAM and it took an average of 9894 milliseconds. None of the <em>long</em> variables are used by the ObjectTrimming class.&nbsp; &nbsp;&nbsp;Neither could they be accessed by other classes, so we decided to remove these fields and rerun the modified benchmark. This time the execution time for the three runs averaged to 6493 milliseconds. That is 52% improvement in application performance. It is unlikely that a class will have a large unused-fields-to-used-fields ratio as in our example above. However, the example above shows that saving 56 bytes of data in a 1K object can have a big impact on memory footprint of the application.</p>
<p>&nbsp;</p>
<p>We used Sun Java SE Runtime Environment (build 1.6.0_06-p-b01) with Java HotSpot Server VM (build 14.0-b09, mixed mode) for the above experiments. The JVM command-line flags used to run the benchmark were: java -server -Xms1175M -Xmx1175M</p>
<p>&nbsp;</p>
<p>As noted above, JVMs cannot necessarily eliminate such unused fields, thus it is the Java Developer's responsibility to optimally define classes.</p>
<p>&nbsp;</p>]]></description>
	</item>
	
	<item>
		<dc:creator>Gary Frost</dc:creator>
		<title>Is String Immutable?</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=109842</link> 
		<pubDate>2009-03-03T10:44:39 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=109842#comments</comments>
		<trackback:ping>4</trackback:ping>
		<description><![CDATA[ <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">In many texts String is cited as the 'gold standard' of Java's various immutable classes.&nbsp;&nbsp;Any google of 'Immutable Java' will invariably reveal examples using String&nbsp;to demonstrate the benefits and characteristics of a good immutable class.&nbsp;&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">From Wikipedia(<a href="http://en.wikipedia.org/wiki/Immutable_object"><span style="color: #007878; text-decoration: none; text-underline: none;">http://en.wikipedia.org/wiki/Immutable_object</span></a>) we get the following definition:-</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;'In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created.'</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">I like this definition as it describes what I had assumed to be the main point: that an instance's state should not be allowed to be modified post-construction.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">However, I recently found myself looking at the String class and came across its hashCode() method :-</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: "Courier New";">public int hashCode() {</span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: "Courier New";"><br /><strong><span style="font-family: "Courier New";">&nbsp;int h = hash;&nbsp;</span></strong><br /><strong><span style="font-family: "Courier New";">&nbsp;if (h == 0) {</span></strong><br /><strong><span style="font-family: "Courier New";">&nbsp;&nbsp;&nbsp;&nbsp; int off = offset;</span></strong><br /><strong><span style="font-family: "Courier New";">&nbsp;&nbsp;&nbsp;&nbsp; char val[] = value;</span></strong><br /><strong><span style="font-family: "Courier New";">&nbsp;&nbsp;&nbsp;&nbsp; int len = count;</span></strong><br /><strong><span style="font-family: "Courier New";">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; len; i++) {</span></strong><br /><strong><span style="font-family: "Courier New";">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; h = 31*h + val[off++];</span></strong><br /><strong><span style="font-family: "Courier New";">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></strong><br /><strong><span style="font-family: "Courier New";">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></strong></span></strong><strong><span style="font-size: 8pt; color: red; font-family: "Courier New";">hash = h;</span></strong><strong><span style="font-size: 8pt; color: red; font-family: "Courier New";"><br /></span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: "Courier New";">&nbsp;&nbsp;&nbsp; }</span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: "Courier New";"><br /><strong><span style="font-family: "Courier New";">&nbsp;&nbsp;&nbsp; return h;</span></strong><br /><strong><span style="font-family: "Courier New";">}</span></strong></span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;"></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">Looking closely we see that this is a classic implementation of the 'lazy evaluation' pattern. Rather than computing the hash value&nbsp;in the constructor, or computing it each time the hashCode() method is called,we compute it once&nbsp;(on the first call to hashCode()) and save the computed value in the hash field.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">We can see this if we read the private hash field reflectively.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: "Courier New";">String helloWorld = "helloWorld";</span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;"></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: "Courier New";">Field field=String.class.getDeclaredField("hash");</span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: "Courier New";"><br /><strong><span style="font-family: "Courier New";">field.setAccessible(true); // because hash field is private</span></strong></span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;"></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: "Courier New";">System.out.println("Before first hashcode call "+field.getInt(helloWorld)); </span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;"></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: "Courier New";">helloWorld.hashCode();</span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;"></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: "Courier New";">System.out.println("After first hashcode call "+field.getInt(helloWorld)); </span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;"></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">If one runs this snippet of code it will output something like</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;"><br /></span><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: "Courier New";">Before first hashcode call&nbsp;0</span></strong><strong><span style="font-size: 8pt; color: #4c4c4c; font-family: "Courier New";"><br /><strong><span style="font-family: "Courier New";">After first hashcode call -1554135584</span></strong></span></strong><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;"></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">So for a String instance which we create, but for which we never call hashCode(), the private hash field will remain 0. It is only changed when we call hashCode(). </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">By Wikipedia's definition I believe String fails the immutablility test.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">The argument&nbsp;might be that we can't observe a&nbsp;String instance in a different state without resorting to a reflective read of String's hash field and because the call&nbsp;to retrieve the state actually modifies it; if we can't observe it changing, then it didn't change. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">This is reminiscent of</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">"If a tree falls in a forest and nobody hears it, did it make a noise?"</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">Or&nbsp;my personal version</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">"If I say something in a room and my wife doesn't hear me, am I still wrong?"</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">However it still seems like String is not immutable.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">In Eric Lippert's blog (<a href="http://blogs.msdn.com/ericlippert/archive/2007/11/13/immutability-in-c-part-one-kinds-of-immutability.aspx"><span style="color: #007878; text-decoration: none; text-underline: none;">http://blogs.msdn.com/ericlippert/archive/2007/11/13/immutability-in-c-part-one-kinds-of-immutability.aspx</span></a>) he refers to this as 'Popsicle Immutability' and&nbsp;although it&nbsp;seems safe, (ignoring reflective access) it does seem incorrect.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana;">&nbsp;</span></p>
<p><span style="font-size: 8pt; color: #4c4c4c; font-family: Verdana; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">So I would like to open up&nbsp;a discussion:&nbsp; Is String immutable?</span></p>]]></description>
	</item>
	
	<item>
		<dc:creator>Tom Deneau</dc:creator>
		<title>Huge Pages and NUMA on Windows&#xae; Operating Systems</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=109407</link> 
		<pubDate>2009-02-23T16:07:12 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=109407#comments</comments>
		<trackback:ping>1</trackback:ping>
		<description><![CDATA[ <table class=" FCK__ShowTableBorders" style="WIDTH: 100%" border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 0in; BORDER-TOP: #ece9d8; PADDING-LEFT: 0in; PADDING-BOTTOM: 0in; BORDER-LEFT: #ece9d8; PADDING-TOP: 0in; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" valign="top">
<table class=" FCK__ShowTableBorders" style="WIDTH: 100%" border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 0in; BORDER-TOP: #ece9d8; PADDING-LEFT: 0in; PADDING-BOTTOM: 0in; BORDER-LEFT: #ece9d8; PADDING-TOP: 0in; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" valign="top">
<div style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0in; BORDER-TOP: medium none; PADDING-LEFT: 0in; PADDING-BOTTOM: 4pt; BORDER-LEFT: medium none; PADDING-TOP: 0in; BORDER-BOTTOM: #8f8f8f 1pt solid">
<div style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0in; BORDER-TOP: medium none; PADDING-LEFT: 0in; PADDING-BOTTOM: 0in; MARGIN: 0in 0in 0pt; BORDER-LEFT: medium none; PADDING-TOP: 0in; BORDER-BOTTOM: medium none">&nbsp;</div>
</div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%"><span style="color: #4c4c4c;">Many Java applications, especially those using large heaps, can benefit from what the operating systems call large or huge pages.&nbsp; In the x86 architecture, these are pages that are larger than the default 4K byte pages, usually 2MB.&nbsp; See the article <a href="http://developer.amd.com/documentation/articles/Pages/2142006111.aspx"><span style="color: #007878; text-decoration: none; text-underline: none;">Supersizing Java: Large Pages on the Opteron Processor</span></a> for a discussion of these huge pages and how to set up your OS and JVM to use them.</span></div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%"><span style="color: #4c4c4c;">Now if you are specifically using huge pages in a multi-processor NUMA environment and you intend to run multiple JVMs each affinitized to a node, which means we are pinning that JVM's threads to one node, then for maximum performance you would like to make sure that the huge pages you allocate for each affinitized&nbsp;JVM's heap are local to that node.&nbsp;&nbsp;&nbsp;A previous blog entry addressed this issue for Linux&reg; operating systems, and here we discuss Windows&reg; operating systems.</span></div>
<div style="MARGIN: 12pt 0in 3pt; LINE-HEIGHT: 115%"><strong><span style="font-size: medium;"><span style="color: #4c4c4c;">Huge Pages and NUMA on Windows</span></span></strong></div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%"><span style="color: #4c4c4c;">On Windows, as described in the Supersizing article, you do not need to (in fact you cannot) reserve the huge pages before an application like a JVM can use them.&nbsp;&nbsp; You just need to enable the user&rsquo;s rights to &ldquo;Lock Pages in Memory&rdquo; and the requesting application will acquire the&nbsp;huge pages at runtime.&nbsp; &nbsp;Note that the allocation policy should thus be different from the Linux allocation policy because the Linux policy happened outside of the process context at page reserve time.</span></div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%"><span style="color: #4c4c4c;">But let&rsquo;s look at what happens at runtime. The JVM or other application makes a VirtualAlloc request to allocate a chunk of memory with a flag to map it to huge pages.&nbsp; The OS really deals in terms of 4K pages.&nbsp; To return a 2MB page it must find 512 contiguous 4K pages and those will then be locked in memory (they are not candidates for paging out).&nbsp; By default, it will look for such a 2MB block in the memory that is local to the requesting processor.&nbsp; If the OS cannot find a free 2MB page in local memory, it will search in memory from other nodes in the system.</span></div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%"><span style="color: #4c4c4c;">Another thing to note is that on Windows, an affinitized process really just has its execution threads bound to a set of processor cores.&nbsp;&nbsp; There is no way to&nbsp;force memory allocation to be&nbsp;affinitized as well.&nbsp; &nbsp;&nbsp; Windows will silently allocate memory for an affinitized process from other nodes if it doesn&rsquo;t have enough memory locally.&nbsp; (Note: this is true even if the newer VirtualAllocExNuma call is used.)&nbsp; So the allocation strategy mentioned above holds for both affinitized and non-affinitized processes.</span></div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%"><span style="color: #4c4c4c;">There is one other complication.&nbsp; This&nbsp;huge page allocation strategy, allocating as much as possible from local memory, and then allocating from remote memory reflects what happens on Windows Server 2008.&nbsp; However, on Windows Server 2003 there is a feature of&nbsp;huge page allocation that makes it less likely to allocate as much as possible from local memory. &nbsp;&nbsp;On Windows Server 2003, the OS gives priority to contiguous blocks of 2M pages whether they are local or not.&nbsp;&nbsp; So even if there is enough total local huge pages, if memory is fragmented enough so that large <em>contiguous </em>blocks are not available on the&nbsp;requesting node, the likelihood of getting local huge page memory will be less on WS2003.&nbsp; </span></div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%"><span style="color: #4c4c4c;">If you do control the memory allocation code for the application you can work around this Windows Server 2003 feature by allocating the huge pages one page at a time.&nbsp; Later releases of the standard JVMs implement this Windows Server 2003 workaround.</span></div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%">&nbsp;</div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%"><strong><span style="color: #4c4c4c;">Measuring whether we can get Local Huge Pages</span></strong></div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%"><span style="color: #4c4c4c;">On either Windows Server 2008 or Windows Server 2003, since the application will just silently allocate memory from other nodes if it doesn&rsquo;t have enough locally, how does one tell if their affinitized process really is getting all their huge pages from local memory?&nbsp; I&rsquo;m not aware of any Windows utility that helps answer this question.&nbsp; In AMD Java Labs we use an internal tool that can tell us how many free huge pages are available on each node.&nbsp; This tool allocates as many huge pages as possible and then looks at the physical address of the pages to tell what node they are on.&nbsp;&nbsp;&nbsp; We can then infer that these pages must have been free and could be acquired by the real application&nbsp;of interest.&nbsp; </span></div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%"><span style="color: #4c4c4c;">I&rsquo;d be interested in hearing whether others know of a Windows utility that helps show where the huge pages can be allocated.</span></div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%"><span style="color: #4c4c4c;">There is some advice however.&nbsp;&nbsp;Since you can&rsquo;t reserve huge pages on Windows, and since the longer a system is up, the more fragmented it&rsquo;s free page pool will be, it follows that if your application's&nbsp;performance depends not only&nbsp;on getting enough huge pages but also on making sure those pages are local to each node, such an&nbsp;application should make its request as early as possible after boot time before the memory gets fragmented.</span></div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%"><span style="color: #4c4c4c;">In a future blog, we plan to address a different memory allocation problem:&nbsp;&nbsp;what if your process is not affinitized but really is running on cores that are distributed across several nodes and it still wants memory usage to be as local as possible?&nbsp; Stay tuned.</span></div>
<div style="MARGIN: 0in 0in 10pt; LINE-HEIGHT: 115%">&nbsp;</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>]]></description>
	</item>
	
	<item>
		<dc:creator>Azeem Jiva</dc:creator>
		<title>Heap settings and reading verbose GC output</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=108618</link> 
		<pubDate>2009-02-11T15:45:39 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=108618#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <p><span style="font-size: small; font-family: verdana,geneva;">Last time we talked about setting up GC flags and how to tune your Java application with the proper GC algorithm.&nbsp; This time we're going to focus on how to properly setup your Java heap to help get the best performance possible.</span></p>
<p><span style="font-size: small; font-family: verdana,geneva;">The easiest way to set your heap would be to set a maximum allowable size and let the JVM handle everything else.&nbsp; By using the flag <span style="font-family: courier new,courier;">"-Xmx"</span> you can set the heap size.&nbsp; Example would be:&nbsp; <span style="font-family: courier new,courier;">"-Xmx1024m"</span> or <span style="font-family: courier new,courier;">"-Xmx1g"</span> both set the maximum heap size to 1gigabyte.&nbsp; Just realize that you don't want to set the maximum heap larger than the available memory in your system.&nbsp; Performance will suffer and you'll be worse off than had you used a smaller heap.</span></p>
<p><span style="font-size: small; font-family: verdana,geneva;">While that's a good start, we can clearly do better than that.&nbsp; The JVM also has this notion of the heap being split in multiple sections.&nbsp; One section is the nursery or young generation where all allocations happen.&nbsp; The other section is the tenured space or old generation where long lived objects get promoted from the nursery.&nbsp; We can tune the size of these spaces for better performance depending on how our application allocates objects.&nbsp; By adding the flag <span style="font-family: courier new,courier;">"-Xmn"</span> you can set the nursery size, and the remaining heap is then allocated to the tenured space.&nbsp; </span></p>
<p><span style="font-size: small; font-family: verdana,geneva;">This tuning of nursery can be an important source of performance improvements as a nursery collection is significantly faster than a full GC.&nbsp; And if your application creates many short lived objects then you would be better off with a larger nursery than tenured space.&nbsp; If only we could somehow calculate how much nursery our application uses we could tune better.&nbsp; Ah, but we can!&nbsp; Just use the flag <span style="font-family: courier new,courier;">"-verbose:gc"</span> which shows each nursery and full GC and how much was collected and how much of the heap is free.&nbsp; And in our case we'll add <span style="font-family: courier new,courier;">"-XX:+PrintGCDetails"</span> which increases the level of detail.&nbsp; As an example see:<br />&nbsp;<br /><span style="font-family: courier new,courier;">[GC [DefNew: 910K-&gt;12K(960K), 0.0003947 secs] 1239K-&gt;341K(5056K), 0.0005048 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]<br />[GC [DefNew: 908K-&gt;13K(960K), 0.0004003 secs] 1237K-&gt;341K(5056K), 0.0005126 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]<br />[GC [DefNew: 909K-&gt;14K(960K), 0.0005241 secs] 1237K-&gt;342K(5056K), 0.0006367 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]<br />[Full GC [Tenured: 3485K-&gt;4095K(4096K), 0.1745373 secs] 61244K-&gt;7418K(63104K), [Perm : 10756K-&gt;10756K(12288K)], 0.1762129 secs] [Times: user=0.19 sys=0.00, real=0.19 secs]</span></span></p>
<p><span style="font-size: small; font-family: verdana,geneva;">Those are examples of the output you'll get when using <span style="font-family: courier new,courier;">"-verbose:gc"</span> and <span style="font-family: courier new,courier;">"-XX:+PrintGCDetails"</span>.&nbsp; What does all of that mean?&nbsp; The first three lines are young generation collections.&nbsp; The Nursery is 960k large and had around 909k worth of data before the collection and around 13k after the collection.&nbsp; The total size of the young generation was 5056k and had around 1237k worth of data before the collection and about 341k after the collection.&nbsp; The last line shows a full collection.&nbsp; The tenured space is 4096k and in this case objects were moved into tenured space but none were collected.&nbsp; This points to a bigger problem, where the tenured space is too small and should be increased to help give better performance.<br />&nbsp;<br />You might have noticed that with <span style="font-family: courier new,courier;">"-XX:+PrintGCDetails"</span> you'll get a bit of output when your application ends that looks like:<br />&nbsp;<br /><span style="font-family: courier new,courier;">Heap<br />&nbsp;def new generation&nbsp;&nbsp; total 960K, used 16K [0x22990000, 0x22a90000, 0x22e70000)<br />&nbsp; eden space 896K,&nbsp;&nbsp; 0% used [0x22990000, 0x22990810, 0x22a70000)<br />&nbsp; from space 64K,&nbsp; 22% used [0x22a70000, 0x22a738b0, 0x22a80000)<br />&nbsp; to&nbsp;&nbsp; space 64K,&nbsp;&nbsp; 0% used [0x22a80000, 0x22a80000, 0x22a90000)<br />&nbsp;tenured generation&nbsp;&nbsp; total 4096K, used 328K [0x22e70000, 0x23270000, 0x26990000)<br />&nbsp;&nbsp; the space 4096K,&nbsp;&nbsp; 8% used [0x22e70000, 0x22ec2328, 0x22ec2400, 0x23270000)<br />&nbsp;compacting perm gen&nbsp; total 12288K, used 152K [0x26990000, 0x27590000, 0x2a990000)<br />&nbsp;&nbsp; the space 12288K,&nbsp;&nbsp; 1% used [0x26990000, 0x269b6390, 0x269b6400, 0x27590000)<br />&nbsp;&nbsp;&nbsp; ro space 8192K,&nbsp; 63% used [0x2a990000, 0x2aea3ae8, 0x2aea3c00, 0x2b190000)<br />&nbsp;&nbsp;&nbsp; rw space 12288K,&nbsp; 53% used [0x2b190000, 0x2b7f83f8, 0x2b7f8400, 0x2bd90000)<br /></span>&nbsp;<br />This shows you the sizes of the various parts of the heap, how large they were and the amount used at the time the application quit.&nbsp; It's a good snapshot of the application's heap requirements at the end of the run and can help you tune.&nbsp;&nbsp; As an example, if the new generation used is close to the total size of the new generation then you should increase the nursery size using <span style="font-family: courier new,courier;">"-Xmn"</span> flag.&nbsp; Again having to garbage collect is expensive, and causes your application to stop executing while the collection happens.&nbsp; The fewer you have to do the faster your application runs.&nbsp; And a full GC is slower than a regular GC.&nbsp; If your application is causing many full GCs to happen and your tenured generation is nearly full then modify the nursery (<span style="font-family: courier new,courier;">-Xmn</span>) such that the nursery is a smaller portion of the total heap size.<br />&nbsp;<br />Stay tuned for part 3 as we discuss how to combine GC flags and heap flags to tune a real application for maximum performance.</span></p>]]></description>
	</item>
	
	<item>
		<dc:creator>Gary Frost</dc:creator>
		<title>Builders instead of Constructors for Immutable Objects.</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=108340</link> 
		<pubDate>2009-02-06T13:36:29 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=108340#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <p>As Java developers, we are&nbsp;familiar&nbsp;with constructors.<span style="mso-spacerun: yes;">&nbsp; </span>We create them every day&nbsp;to get our objects into a usable state.&nbsp;<span style="font-size: 8pt; "> </span></p>
<p style="margin: 0in 0in 0pt; line-height: 20.4pt;">Consider a simple <strong>Rectangle </strong>class's constructor.</p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><span><strong>class Rectangle{</strong></span></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><span><strong>private int x,y,w,h; // x, y, width and height</strong></span></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><span><strong>Rectangle(int _x, int _y, int _w, int _h){</strong></span></span></p>
<p style="padding-left: 60px; "><span><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>// I</strong></span><a style="mso-comment-reference: SA_1; mso-comment-date: 20090204T1558;"><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>ntialize</strong></span></a><span style="font-size: 8pt;"><span style="mso-special-character: comment;"><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>&nbsp;</strong></span></span></span></span><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><span><strong> x, y, w and&nbsp;h</strong></span></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><span><strong>}</strong></span></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><span><strong>}</strong></span></span></p>
<p>As API developers, we are always&nbsp;attempting to&nbsp;balance functionality and convenience. Even this fairly straightforward class can become confusing&nbsp;when&nbsp;we try to provide convenient alternative constructors.&nbsp;</p>
<p>If, for example, our Rectangle's <strong>w </strong>and <strong>h </strong>were usually 100 we may be tempted to create a constructor which&nbsp;defers to the original constructor with default values.</p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Rectangle(int _x, int _y){</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>this(_x, _y, 100, 100);</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>}</strong></span></p>
<p>One might&nbsp;also allow a default for when <strong>h</strong> and <strong>w </strong>are 100 and <strong>x </strong>and <strong>y&nbsp;</strong>are 0</p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Rectangle(){</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>this(0, 0, 100, 100);</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>}</strong></span></p>
<p>Things get tricky when we then try to create&nbsp;a constructor where we specify the <strong>w </strong>and <strong>h </strong>and take the default <strong>x </strong>and <strong>y</strong>.</p>
<p>We&nbsp;might be tempted to try to add this constructor</p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Rectangle(int _w, int _h){</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>this(0, 0, _w, _h);</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>}</strong></span></p>
<p>Sadly, this constructor is now ambiguous and will result in a compile failure. If you look at <strong>Rectangle(int _x, int _y), </strong>&nbsp;it is the same. The compiler does not care&nbsp;what the variable names are, there can only be one constructor taking <strong>(int,int).</strong></p>
<p>It would be great if&nbsp;Java had a syntax for disambiguating the arguments to a method or <a style="mso-comment-reference: SA_2; mso-comment-date: 20090204T1558;">constructor</a><span class="MsoCommentReference"><span style="font-size: 8pt;"><span style="mso-special-character: comment;"><span style="font-family: Times New Roman;">&nbsp;</span></span></span></span>.</p>
<p><strong><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;">Rectangle r = new Rectangle(width:100, x:20);</span></strong></p>
<p>We&nbsp;could specify the arguments in any order and the compiler would create a synthetic constructor which matched&nbsp;the arguments; alas Java cannot do this although&nbsp;we will see later how we can get close to this.</p>
<p>Of course, if we did not want our Rectangle to be immutable, we can abandon&nbsp;having multiple&nbsp;constructors and&nbsp;just use mutators.</p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Rectangle r = new Rectangle(); // defaults x,y = 0 and width,height=100</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>r.setWidth(500);</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>r.setHeight(500);</strong></span></p>
<p>This is probably the best approach for most classes; it is a little verbose but it is easy to understand.</p>
<p>However, for immutable classes we need another approach.&nbsp;&nbsp;<span style="font-size: 8pt; "> </span></p>
<p style="margin: 0in 0in 0pt; line-height: 20.4pt;">Thankfully,&nbsp;we&nbsp;can use a&nbsp;builder pattern.</p>
<p>If&nbsp;we&nbsp;define an inner static class within <strong>Rectangle&nbsp;</strong>called <strong>Builder&nbsp;</strong>which we can mutate into an appropriate state (using regular setters), we can then pass this builder into <strong>Rectangle's </strong>constructor and let the constructor pull its own state from the builder.</p>
<p>Sounds more complicated than it is. So lets see what this looks like:</p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>class Rectangle{</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>private int x,y,h,w;</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>public static class Builder{</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>protected int x,y,h,w;</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>void&nbsp;setW(int _w){w = _w; }</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>void setX(int _x){x = _x;}</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>void setY(int _y){y = _y;}</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>void&nbsp;setH(int _h){h = _h;}</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>}</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>public&nbsp;Rectangle(Builder _builder){</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>x=_builder.x;</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>y=_builder.x;</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>w=_builder.w;</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>h=_builder.h;</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>}</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>}</strong></span></p>
<p>So now we can create an instance of <strong>Rectangle.Builder </strong>and&nbsp;mutate it.</p>
<p>Then we can create a <strong>Rectangle </strong>by passing the <strong>Rectangle.Builder </strong>to the constructor.</p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Rectangle.Builder&nbsp;builder = new Rectangle.Builder();</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>builder.setW(500);</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>builder.setH(1000);</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Rectangle rectangle = new Rectangle(builder);</strong></span></p>
<p>We can make code less verbose by&nbsp;using what I will call 'chainable mutators' to the builder.&nbsp; Unlike traditional mutators, we avoid using the traditional <strong>setX() </strong>style for field&nbsp;<strong>x</strong><strong><span style="font-weight: normal; mso-bidi-font-weight: bold;">;</span></strong><strong></strong>instead we just name the mutator the same as the field (so we use&nbsp;<strong>x() </strong>instead of<strong>setX()</strong>). We also return&nbsp;'this' from our mutator so that subsequent mutator calls&nbsp;can be chained together.</p>
<p>Our builder mutators now look like this:</p>
<p><strong>Builder w(int _w){</strong></p>
<p style="padding-left: 30px; "><strong>w = _w;</strong></p>
<p style="padding-left: 30px; "><strong>return(this);</strong></p>
<p><strong>}</strong></p>
<p><span>And so now we can use:</span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Rectangle.Builder builder = new Rectangle.Builder();</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>builder.w(200).h(200).x(5).y(5);</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Rectangle rectangle = new Rectangle(builder);</strong></span></p>
<p>We can construct and chain in one line:</p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Rectangle.Builder&nbsp;builder = new Rectangle.Builder().w(200).h(200).x(5).y(5);</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Rectangle rectangle = new Rectangle(builder);</strong></span></p>
<p>Or we can avoid declaring the temporary builder&nbsp;object altogether.</p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Rectangle r = new Rectangle(new Rectangle.Builder().w(200).h(200).x(5).y(5));</strong></span></p>
<p>Now&nbsp;we have the safety of immutable <strong>Rectangles </strong>with a mechanism for getting immutable objects into a reasonable state.</p>
<p>At this point we can make two more enhancements.</p>
<p style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; line-height: 20.4pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level1 lfo1; tab-stops: list .5in;"><span style="font-size: 8pt; mso-fareast-mso-bidi-"><span style="mso-list: Ignore;">1.<span style="font-family: ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Add&nbsp;a static <strong>build()</strong> method&nbsp;to <strong>Rectangle </strong>to construct the <strong>Rectangle.Builder</strong>.</p>
<p style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; line-height: 20.4pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-list: l0 level1 lfo1; tab-stops: list .5in;"><span style="font-size: 8pt; mso-fareast-mso-bidi-"><span style="mso-list: Ignore;">2.<span style="font-family: ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Add a <strong>commit()</strong> method to the <strong>Rectangle.Builder</strong> to construct the <strong>Rectangle</strong>.</p>
<p>Our code now looks like this:</p>
<p><strong>class Rectangle{</strong></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>private int x,y,h,w;</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>public static class Builder{</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>protected int x,y,h,w;</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Builder w(int _w){w = _w; return(this);}</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Builder x(int _x){x = _x; return(this);}</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Builder y(int _y){y = _y; return(this);}</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Builder h(int _h){h = _h; return(this);}</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Rectangle commit(){return new Rectangle(this);}</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>}</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>private Rectangle(Builder _builder){</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>x=_builder.x;</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>y=_builder.x;</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>w=_builder.w;</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>h=_builder.h;</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>}</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>Builder build(){</strong></span></p>
<p style="padding-left: 60px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>return(new Builder());</strong></span></p>
<p style="padding-left: 30px; "><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>}</strong></span></p>
<p><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;"><strong>}</strong></span></p>
<p><span>And we can create a Rectangle using:</span></p>
<p><strong><span style="font-weight: normal! important; font-size: 13px! important; color: #993399; font-family: Courier New, Courier, mono;">Rectangle r = Rectangle.build().w(200).h(200).x(5).y(5).commit();</span></strong></p>
<p>This pattern is certainly&nbsp;not new. It borrows from the traditional builder pattern and from what Martin Fowler refers to as a 'fluent interface': (<a href="http://martinfowler.com/bliki/FluentInterface.html">http://martinfowler.com/bliki/FluentInterface.html</a>).</p>
<p><span style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">We have been using this pattern in&nbsp;internal projects and would be interested to hear how folks feel about combining the builder pattern with chainable mutators for constructing immutable objects.</span></p>]]></description>
	</item>
	
	<item>
		<dc:creator>Azeem Jiva</dc:creator>
		<title>Which Java GC Collector is right for you?</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=108148</link> 
		<pubDate>2009-02-03T16:43:58 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=108148#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <p><span>Tuning GC flags seems like it should be difficult and error prone, but following a few easy steps can help you get improved application performance with only a few minutes of your time.<span style="mso-spacerun: yes;"> </span>The first step is to decide what sort of application you are trying to tune.<span style="mso-spacerun: yes;"> </span>Is your application throughput bound and needs to perform as many transactions as possible in the shortest time period?<span style="mso-spacerun: yes;"> </span>Or is your application latency bound where each transaction must finish in a specified time period?</span></p>
<p><span><span>I'm going to be using the Sun HotSpot JVM flags as my examples but both Oracle's JRockit and IBM's J9 have similar flags.<span style="mso-spacerun: yes;"> </span>See the appropriate documentation on Oracle's or IBM's site, respectively.<span style="mso-spacerun: yes;"> </span></span><br /><br /><span>Traditionally most applications are throughput bound and are not sensitive to GC pause times.<span style="mso-spacerun: yes;"> </span>Throughput bound applications are better suited to a fast collection where the JVM can pause Java execution for GC.<span style="mso-spacerun: yes;"> </span>There is another class of applications that require a certain guarantee of how long the GC pauses will take.<span style="mso-spacerun: yes;"> </span>These applications are popular in the financial sector and other related fields.<span style="mso-spacerun: yes;"> </span>These applications require that the JVM ensures that individual transactions finish within a certain time period, usually less than 10ms.<span style="mso-spacerun: yes;"> </span></span><br /><br /><span>Let's start with the most common applications first and target throughput applications. The first flag to enable is "<span style="font-family: courier new,courier;">-XX:+UseParallelOldGC</span>" which enables a parallel version of both the young generation collector and the old generation collector.<span style="mso-spacerun: yes;"> </span>By default the number of threads is some variant of the number of processors that changes depending on the version of Java.<span style="mso-spacerun: yes;"> </span>We should change that to some reasonable default depending on the number of cores that you have available on your system and more importantly the number of cores you want to dedicate to this application.<span style="mso-spacerun: yes;"> </span>In this example I'm going to assume the system has two processors and eight cores (2p/8c).<span style="mso-spacerun: yes;"> </span>Since the application I want to run is exclusively dedicated to the system I'm also going to set aside all eight cores to garbage collection.<span style="mso-spacerun: yes;"> </span>Now this isn't as bad as you realize since the ParallelGC is a "Stop the world" collector and your Java application won't make forward progress until the collection has completed.<span style="mso-spacerun: yes;"> </span>To modify the number of threads use the flag:<span style="mso-spacerun: yes;"> </span>"<span style="font-family: courier new,courier;">-XX:ParallelGCThreads=#</span>" where # is the number of threads in our case 8.</span></span></p>
<p><span><span>Once you've enabled those flags test the application out and see how much performance you've gained.<span style="mso-spacerun: yes;"> </span>Ideally your application should now run faster and have shorter garbage collection pause times.<span style="mso-spacerun: yes;"> </span>What about other applications that are latency driven rather than performance driven?<span style="mso-spacerun: yes;"> </span>For those applications we need to enable the low pause or concurrent collector.<span style="mso-spacerun: yes;"> </span>To enable the concurrent collector we use the flag "<span style="font-family: courier new,courier;">-XX:+UseConcMarkSweepGC</span>" which enables the concurrent collector.<span style="mso-spacerun: yes;"> </span>But since we care about latency and don't want GC time to dominate over application CPU use time we need to enable the incremental mode for CMS by using "<span style="font-family: courier new,courier;">-XX:+CMSIncrementalMode</span>".<span style="mso-spacerun: yes;"> </span>These two flags only get maximum impact when the JVM knows what the collector is doing and is able to profile and change parameters.<span style="mso-spacerun: yes;"> </span>To enable that feature use "<span style="font-family: courier new,courier;">-XX:+CMSIncrementalPacing</span>".<span style="mso-spacerun: yes;"> </span></span></span></p>
<p><span>Now rerun your application and see if the response time for your application has increased.<span style="mso-spacerun: yes;"> </span>In most cases while response time may have increased, your throughput may have dropped, but whether that tradeoff is acceptable is a decision only you can make.<span style="mso-spacerun: yes;"> </span>Stay tuned next time as we explore how to use GC logs to tune heap sizes and control GC performance.</span></p>
<p><span>-------------------------<br /><br />Azeem Jiva, MTS Software Engineer </span></p>]]></description>
	</item>
	
	<item>
		<dc:creator>Ben Pollan</dc:creator>
		<title>SD Times article about AMD and Java</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=107718</link> 
		<pubDate>2009-01-28T14:57:21 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=107718#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <p><span style="font-size: 8pt; color: #4c4c4c; font-family: "Verdana","sans-serif"; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">
<p><span style="font-size: 8pt; color: #4c4c4c;">Happy New Year, everyone!</span></p>
<div style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c;">Well, I may have managed to avoid watching "It's a Wonderful Life" last month, but I can't say the same about avoiding holiday cookies.&nbsp; I've just come off of enough of my seasonal sugar high to be able to construct actual sentences, so that I can&nbsp;tell you about the great trip that I had to NY on 12/17.</span></div>
<div style="margin: 0in 0in 0pt; line-height: 20.4pt;">&nbsp;</div>
<div style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c;">First, I had a good chat with David Worthington of the SD Times, who put together an <a href="http://www.sdtimes.com/AMD_LOOKS_FOR_WAYS_TO_OPTIMIZE_JAVA_ON_SHANGHAI_/About_JAVA_and_MULTICORE_and_AMD/33123"><span style="color: #007878; text-decoration: none; text-underline: none;">article discussing AMD's Java optimization efforts</span></a>.&nbsp; It highlights some of the short and long term optimization goals that we have.</span></div>
<div style="margin: 0in 0in 0pt; line-height: 20.4pt;">&nbsp;</div>
<div style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c;">That evening, I presented "Run Anywhere: The Hardware Platform Perspective" at the NY JavaSIG.&nbsp; Mainly, it's an introduction to the work that we do in the Java Labs, and an overview of tuning options from a hardware point-of-view.&nbsp; Many thanks to Google, who hosted the space for the meeting,&nbsp;to the great NY JavaSIG audience, and a special shout out to Eric Bruno, who <a href="http://dobbscodetalk.com/index.php?option=com_myblog&show=Inside-AMD-Java-Labs.html&Itemid=29">blogged about&nbsp;it</a> on Dr. Dobb's CodeTalk</span></div>
<div style="margin: 0in 0in 0pt; line-height: 20.4pt;"></div>
<div style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c;">Overall, a successful trip.&nbsp; I even got to see some snow before getting back to warmer climes.</span></div>
<div style="margin: 0in 0in 0pt; line-height: 20.4pt;"></div>
<div style="margin: 0in 0in 0pt; line-height: 20.4pt;"><span style="font-size: 8pt; color: #4c4c4c;">Ben</span></div>
</span></p>]]></description>
	</item>
	
	<item>
		<dc:creator>Tom Deneau</dc:creator>
		<title>Huge Pages and NUMA issues on Linux&#xae;</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=107415</link> 
		<pubDate>2009-01-23T15:18:46 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=107415#comments</comments>
		<trackback:ping>2</trackback:ping>
		<description><![CDATA[ <div style="MARGIN: 12pt"><span style="FONT-SIZE: 8.5pt">Many Java applications, especially those using large heaps, can benefit from what the operating systems call large or huge pages.&nbsp; In the x86 architecture, these are pages that are larger than the default 4K byte pages, usually 2MB.&nbsp; See the article <a title="http://developer.amd.com/documentation/articles/Pages/2142006111.aspx" href="http://developer.amd.com/documentation/articles/Pages/2142006111.aspx"><span style="color: windowtext; text-decoration: none; text-underline: none;">Supersizing Java: Large Pages on the Opteron Processor</span></a> for a discussion of these huge pages and how to set up your OS and JVM to use them.</span></div>
<div style="MARGIN: 12pt"><span style="FONT-SIZE: 8.5pt">But what if you are specifically using huge pages in a NUMA environment and you intend to run multiple JVMs each affinitized to a node?&nbsp; For maximum performance you would like to make sure the huge pages are balanced across the nodes of the system. &nbsp;In the following we discuss some finer points of huge pages and NUMA.&nbsp; The behavior and utilities available are different across Linux&reg; and Windows&reg;-based systems.</span></div>
<div style="MARGIN: 12pt"><span style="FONT-SIZE: 8.5pt">Huge Pages and NUMA on Linux<span style="font-size: 8.5pt; font-family: 'Verdana','sans-serif'; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">&reg;</span></span></div>
<div style="MARGIN: 12pt"><span style="FONT-SIZE: 8.5pt">On Linux, as described in the Supersizing article, you have to first reserve the huge pages (by a command such as echo nnn &gt;/proc/sys/vm/nr_hugepages) before an application like a JVM can use them.&nbsp; Since huge pages need to be locked in memory, the write to nr_hugepages actually allocates the pages, and holds them in a pool.&nbsp; This reserved huge page memory cannot then be used for small pages.&nbsp; By reading back nr_hugepages, you can see whether you got the number of huge pages you asked for.&nbsp; (Note that you won&rsquo;t see an error message on the write, so you won&rsquo;t know how many huge pages you got unless you read it back).&nbsp; This is all fine, but what if we want to know not only how many huge pages we got, but also what nodes they were attached to?</span></div>
<div style="MARGIN: 12pt"><span style="FONT-SIZE: 8.5pt">This information is available thru the command<br />&nbsp;&nbsp;&nbsp;&nbsp; cat /sys/devices/system/node/node*/meminfo.<br /></span><span style="FONT-SIZE: 8.5pt">For each node, you are shown the HugePages_Total and the HugePages_Free.&nbsp; So after reserving thru nr_hugepages, you should see the HugePages_Free on the nodes in the system adding up to the total number of hugepages you asked for.&nbsp; And you would want those HugePages_Free to be distributed evenly across the nodes.</span></div>
<div style="MARGIN: 12pt"><span style="FONT-SIZE: 8.5pt">The default allocation policy (maybe the only one) is to go from node to node in a round robin fashion looking for a free 2MB chunk of memory and placing it in the pool.&nbsp; However, the important point is that if one node has no more free 2MB chunks of contiguous memory available, the reservations will continue on other nodes until the required nr_hugepages are reserved or until no more hugepages can be found on any node.&nbsp; &nbsp;This makes sense because when you write to nr_hugepages, the system doesn&rsquo;t know whether you&rsquo;re later going to use those huge pages from affinitized processes or not.</span></div>
<div style="MARGIN: 12pt"><span style="FONT-SIZE: 8.5pt">The above logic holds true when you&rsquo;re increasing the number of huge pages.&nbsp; However,&nbsp; if you&rsquo;re reducing the number of huge pages, the behavior is less sensible in that the OS will free up as many pages as possible from node 0, then as many as possible from node 1, etc. until you&rsquo;ve freed up the required number of pages.&nbsp; Thus, if you want to reduce the number of huge pages and still have them balanced across nodes, first reduce them to zero, and then increase them to the number you really want.</span></div>
<div style="MARGIN: 12pt"><span style="FONT-SIZE: 8.5pt">On Linux, when an application that has been affinitized using &ndash;membind &nbsp;wants to allocate a huge page from the pool, it is only allowed to allocate from the pool local to the node (or nodes) it&rsquo;s been affinitized to.&nbsp; If the application asks for more pages than are available on that node, it will fail rather than just allocate an unused page from the pool of some other node.&nbsp; In a way this is good because if you went to the trouble of affinitizing you wanted the pages to be local and there are advantages to failing early and letting you fix the page allocation problem rather than running with non-local pages at possibly lower performance.</span></div>
<div style="margin: 12pt;"><span style="FONT-SIZE: 8.5pt">In general huge page reservation should be done as early as possible before the memory gets fragmented which would result in fewer contiguous 2MB chunks being available.&nbsp; But the methods above can let you know whether a later reservation will meet your needs or not.</span></div>
<p><span style="FONT-SIZE: 8.5pt">In our next entry, we&rsquo;ll look at how Windows-based systems handle huge pages and NUMA.&nbsp;Meanwhile, I&rsquo;d be interested in cases where people have actually used huge pages and affinity on Linux.</span></p>]]></description>
	</item>
	
	<item>
		<dc:creator>Vasanth Venkatachalam</dc:creator>
		<title>Inlining Information Hidden in the Hotspot JVM - Part II</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=106709</link> 
		<pubDate>2009-01-12T18:31:22 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=106709#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <div class="ExternalClass53F0DF1196D84B268F6F7B6A18FFC5A3">
<div>Tracing a performance problem back to source code is a common task in performance analysis. Tools that automate this task can save engineers plenty of manual labor. Though there are many Java tools that perform source mapping, they arguably have limited accuracy because JVMTI, the interface that they rely on, lacks information about inlining decisions. JVMTI does not know whether the code that the JVM generates for a compiled method belongs to the method itself or to another method that was inlined into it.</div>
<div>&nbsp;</div>
<div>In a previous blog article ("Inlining Information Hidden in the Hotspot JVM"), I mentioned that we have found a way to expose to tool writers the inlining information that is inside the Hotspot JVM, paving the way for more accurate source mapping. However, I didn't describe how we implemented our approach. The way you solve a software problem can open the door to solving other software problems than the one that you had originally targeted.</div>
<div>&nbsp;</div>
<div>Initially, we wanted to solve the problem of improved source mapping. We knew that the Hotspot JVM contained information about inlining decisions, and that this information was unavailable to tool writers. We wanted to expose this information to tool writers.</div>
<div>&nbsp;</div>
<div>Naturally, the first place to look was the JVMTI interface, since this is the interface that is used to pass information between the JVM and external tools. Through this interface, the JVM emits events (such as CompiledMethodLoad and ClassFileLoadEvent) to inform external tools about the different phases that it is in. For example, whenever it compiles a method, it emits a CompiledMethodLoadEvent, which contains information about the method that was compiled. External tools (e.g., JVMTI agents) can process these events to do things like printing the names of the methods that the JVM compiled, or the names of the classes that were loaded.</div>
<div>&nbsp;</div>
<div>The interesting thing about the CompiledMethodLoad callback is that it contains a void pointer that the JVM does not use. This led us to our solution: Whenever a method is compiled, we could simply attach its inlining information to this pointer, thereby allowing this information to be passed outside the JVM via the CompiledMethodLoad event.</div>
<div>&nbsp;</div>
<div>What's nice about this mechanism is that it doesn't alter the JVMTI spec, and that it is reusable. Although this mechanism allows us to expose inlining information, we could in the future use it to expose other kinds of information, such as register allocation information or information about optimization decisions. So while our changes benefit source-to-address mapping, they can have more widespread applications.</div>
<div>&nbsp;</div>
<div>What other applications do you see benefiting from our approach? And to those of you who are writing Java tools, what additional information do you wish you had, which is currently unavailable to you since it is inside the JVM?</div>
</div>]]></description>
	</item>
	
	<item>
		<dc:creator>Vasanth Venkatachalam</dc:creator>
		<title>Inlining Information Hidden in the Hotspot JVM</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=106707</link> 
		<pubDate>2009-01-12T18:28:25 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=106707#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <p>
<div>When troubleshooting a performance problem, we often want to know what part of a program's source code caused the problem in the first place.&nbsp;Luckily, in&nbsp;the case of Java source, there are numerous tools that simplify this process by&nbsp;displaying the compiled code and source code for a method side-by-side. These Java analysis tools rely on the Java Virtual Machine Tools Interface (JVMTI) to capture information about how a program's source code maps to the compiled code.</div>
<div>&nbsp;</div>
<div><a href="http://java.sun.com/j2se/1.5.0/docs/guide/jvmti/jvmti.html">http://java.sun.com/j2se/1.5.0/docs/guide/jvmti/jvmti.html</a></div>
<div>&nbsp;</div>
<div>One limitation of JVMTI is that when a method is compiled, the methods that it calls may be inlined into its own body.</div>
<div>For example, consider the following code snippet:</div>
<div>&nbsp;</div>
<div>int foo(int j)</div>
<div>{</div>
<div>&nbsp;</div>
<div>return j+1;</div>
<div>&nbsp;</div>
<div>}</div>
<div>&nbsp;</div>
<div>public static void main(String[] args)</div>
<div>{</div>
<div>&nbsp;&nbsp; for(int i = 0; i &lt; 3; i++)</div>
<div>&nbsp;&nbsp;&nbsp;{</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j += foo(j);</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
<div>}</div>
<div>&nbsp;</div>
<div>The JVM is smart enough to see that the work being done inside the function foo is trivial (the incrementing of a counter), and that it&nbsp;would be&nbsp;less expensive to execute the code for foo as part of the code for main than to execute a separate procedure call to foo.&nbsp; As a result, the JVM will inline the code for foo into the code it generates for main. (Actually, the JVM&nbsp;could go a step further in this simple example, and directly compute the final value of j.)</div>
<div>&nbsp;</div>
<div>Inlining raises a problem for JVMTI because JVMTI has no way of knowing whether the code generated for a method belongs to the method itself or to other methods that were called and inlined into it. This makes it difficult to tell which&nbsp;part of a program's source code is ultimately&nbsp;responsible for a performance bottleneck.</div>
<div>&nbsp;</div>
<div>At JavaLabs, we are prototyping a software process to solve this problem. The main idea is that the Hotspot JVM contains hidden information about methods that are inlined.&nbsp;We&nbsp;have found a way to make this information&nbsp;visible to external tools through the JVMTI layer, without altering the JVMTI specification.</div>
<div>&nbsp;</div>
<div>You may&nbsp;be wondering what our secret is. When the Hotspot JVM compiles a method, it creates a list of records that tell you what methods are on the compile time stack at every program address. This indirectly tells&nbsp;you what methods have been inlined; these are the methods that don't have call instructions in the disassembled code. You can view this inlining information by running Hotspot in debug mode with the command line option,&nbsp;-XX:+PrintNMethods.</div>
<div>&nbsp;</div>
<div>We have extended Hotspot to pass this stack information into the CompiledMethodLoad JVMTI callback. As a result, tools can use this information to produce better source-to-address mapping.</div>
<div>&nbsp;</div>
<div>&nbsp;As a next step, we'll be&nbsp;extending tools that AMD has developed (e.g., CodeSleuth, AMD Code Analyst) to&nbsp;exploit this inlining information.&nbsp;We also&nbsp;plan to contribute our Hotspot changes to the Open JDK. Stay tuned for more details.</div>
</p>]]></description>
	</item>
	
	<item>
		<dc:creator>Ben Pollan</dc:creator>
		<title>Austin Java User Group Presentation</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=102878</link> 
		<pubDate>2008-11-10T19:27:30 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=102878#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <div style="margin: 5pt 0in;">The first question that I was asked this year at JavaOne by an attendee was, &ldquo;why in the world is AMD here?&rdquo; Of course, such questions provided us with a good lead into just what it is that we do in the Java Labs. But, it also highlights a common theme among Java developers. The Java runtime provides such a good level of abstraction from the hardware for their apps, that they have an out of sight / out of mind view of us. It made me realize that when it comes to the Java community, we need to get out more. Mainly, we need to work with them to re-connect the dots between our platform and their apps, because even when your life is all about software, the hardware matters. A lot.</div>
<div style="margin: 5pt 0in;">The Java User Groups (JUGs) provide a good opportunity to meet with those developers. And what better place to start than the group in Austin, the Java Labs&rsquo; home town. On the evening of 10/28, I presented <em>Run Anywhere: A Hardware Platform Perspective</em>. The gist was to tell the group about our support of the community via the Java Labs, guide them with some hardware-oriented optimizations, and give them a potential glimpse of the future with AMD&rsquo;s planned platform improvements for Java.</div>
<div style="margin: 5pt 0in;">The meeting went well. We had a lot of positive feedback from Java developers that represented a wide range of companies. Some want us to present to specific groups at their work. Others are using this as an opportunity to open the conversation with us about many aspects of Java, to leverage the role that AMD plays there. We look forward to working with those teams.</div>
<div style="margin: 5pt 0in;">Many thanks to the Austin JUG for the opportunity! We&rsquo;ll be back.</div>
<div style="margin: 5pt 0in;">Ben</div>]]></description>
	</item>
	
	<item>
		<dc:creator>Tom Deneau</dc:creator>
		<title>Runtime configuration parameters for bootclasspath library routines</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=102251</link> 
		<pubDate>2008-10-29T16:13:19 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=102251#comments</comments>
		<trackback:ping>3</trackback:ping>
		<description><![CDATA[ <p>Assume for some strange reason that you want to modify one of the base Java library routines, for example, java.util.HashMap.&nbsp; Since these classes are part of booting up Java, you'll need to add your new jars to the bootclasspath, rather than the normal class path.&nbsp; Adding -Xbootclasspath/p:your.jar as a JVM option solves this purpose.&nbsp; It prepends your.jar to the original bootclasspath.</p>
<p>Now imagine you want to parameterize your new library routine, perhaps tweaking a value for some performance testing as part of a runtime script.&nbsp; For most library routines a system property&nbsp;can be used.&nbsp; Your library source would then have something like</p>
<pre class="code">   final static int myParam = Integer.getInteger("myParam", 6);</pre>
<p>where 6 is the default if the property doesn't exist and you would use -DmyParam=nnn on the JVM command line to specify other values.</p>
<p>However, some classes (java.util.HashMap, for example) are loaded early enough in the boot process that you can't use Properties in that class, because the&nbsp;Properties class&nbsp;itself depends on the class like HashMap that is&nbsp;being loaded.</p>
<p>One way to work around this without rebuilding your HashMap class on every run is to have your parameterized HashMap get the myParam value from a separate very simple class.&nbsp; You would then build a different version of the simple class for each different parameter value you wanted to use.&nbsp; Your library source would then have something like</p>
<pre class="code">   final static int myParam = myPackage.MySimpleClass.param;</pre>
<p>and MySimpleClass.java would contain</p>
<pre class="code">   Package myPackage;<br />
   public class MySimpleClass {public static int param = 2;}</pre>
<p>At javac compile time, you would compile HashMap against some random version of MySimpleClass&nbsp; but at run time you could add to the bootclasspath a .jar file containing the actual MySimpleClass parameter you want to use.</p>
<p>Note that in the above examples, we declared myParam itself to be final.&nbsp; In some cases, this helps the JIT compiler generate better code in our new class.&nbsp; Note also that we were careful NOT to make the param in MySimpleClass final.&nbsp; If this had been final, the javac compiler would have propogated the final constant over to our class, and then our override with a different class at run time would have no effect.</p>
<p>I'm interested in&nbsp;techniques others may have used to solve this problem.</p>]]></description>
	</item>
	
	<item>
		<dc:creator>Shrinivas Joshi</dc:creator>
		<title>AMD&apos;s first official OpenJDK contribution</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=101815</link> 
		<pubDate>2008-10-21T16:57:37 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=101815#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <div style="text-align: left;">When we&nbsp;<a href="http://forums.amd.com/devblog/blogpost.cfm?threadid=92481&catid=313">announced</a>&nbsp;our participation in OpenJDK community, we promised that you would soon start seeing contributions from AMD to the OpenJDK project. Well, here it is: our first official OpenJDK contribution has been accepted and released in OpenJDK build 1.7.0-ea-b29 32-bit JVM.</div>
<div style="text-align: left;">&nbsp;</div>
<div style="text-align: left;">This contribution is an optimization&nbsp;designed to improve performance of bitwise left shift operations. In&nbsp;a 32-bit platform the OpenJDK JVM uses SHLD instruction followed by SHL instruction to perform left shift operation on values of <em>long </em>data type. Since <em>long </em>data type values are 64-bit wide, the 32-bit JVM&nbsp;has to use&nbsp;this 2 step process for carrying out left shift operation. For example if the JVM needs to left shift a value by 2 places then&nbsp;first it shifts-in the high 2 bits of the lower 32 bits part of the 64-bit wide <em>long </em>value into the higher 32 bits part using the SHLD instruction. In the second step&nbsp;the JVM needs to left shift the lower 32 bits part by 2 places. Here it can use SHL instruction as it does not need to care about shifted-out bits. The net effect of this 2 step process&nbsp;is that a 64-bit <em>long </em>data type value is shifted left by 2 places. These 2 steps are shown&nbsp; in the diagram below:</div>
<div style="text-align: center;"><img style="vertical-align: middle;" src="http://developer.amd.com/PublishingImages/SHLD_SHL.bmp" alt="" width="470" height="271" /></div>
<div style="text-align: left;">You can find out more about SHLD and SHL instruction in the "<a href="http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/24594.pdf">AMD64 Architecture Programmer's Manual Volume 3: General-Purpose and System Instructions</a>". The form of SHLD instruction generated by the OpenJDK JVM here is a VectorPath instruction with a latency of 4 cycles.&nbsp;As compared to DirectPath instructions, VectorPath instructions appear to&nbsp;consume more resources and&nbsp;do not promote optimal decode bandwidth usage of the processor. Thus, from a performance perspective, it is important to execute as few VectorPath instructions as possible. So our goal here is to replace SHLD instruction with DirectPath instructions.
<div>&nbsp;</div>
<div>Bitwise left shift operations essentially performs multiplication of the value being shifted by 2 to the power of number of shift places. For example bitwise left shift of value 'n' by 2 places is equivalent to n&nbsp;multiplied by&nbsp;2 to the power of 2, bitwise left shift of value 'n' by 3 places is equivalent to n&nbsp;multiplied by&nbsp;2 to the power of&nbsp;3 and so on and so forth. An easy way of achieving this effect is addition operation. Thus, if we add the 64-bit <em>long </em>data type value to itself, we will&nbsp;left shift the value by 1 place; if we do the addition twice, we will&nbsp;left shift the value by 2 places and so on and so forth. Now on a 32-bit platform we need to use ADD and ADC instructions to correctly perform 64-bit addition. ADD and ADC instructions are DirectPath Single instructions with a latency of 1 cycle.</div>
<div>&nbsp;</div>
<div>The code changes in this OpenJDK contribution&nbsp;are designed to replace SHLD and SHL instructions with appropriate number of ADD and ADC instructions. The exact code changes can be found <a href="http://webrev.invokedynamic.info/kvn/6708714/index.html">here</a>. Note that these changes are only applicable to those cases where a <em>long </em>value is being shifted by 1, 2 or 3 places. Beyond 3 places the number of ADD and ADC instructions required increases to a level where benefits of removing VectorPath instructions are overshadowed by the number of cycles required to execute ADD and ADC instructions and also the increase in code size introduced by multiple ADD and ADC instructions.</div>
<div>&nbsp;</div>
<div>This optimization can be enabled or disabled by using&nbsp;-XX:+UseNewLongLShift and -XX:-UseNewLongLShift respectively. On a micro-benchmark which performed left shift by 1, 2 and 3 operations&nbsp;on <em>long </em>values ranging between&nbsp;0 to 999999999 we saw around 33% improvement in the performance.</div>
<div>&nbsp;</div>
<div>The system under test here had following configuration: 2 chips, 8 cores, 4 cores per chip, Quad-Core AMD Opteron(tm) Processor&nbsp;8356, 2294 MHz, 8 GB DDR2 667 MHz, 120 GB WDC HDD, 64-bit Microsoft Windows Server Enterprise SP1, Java(TM) SE Runtime Environment (build 1.7.0-ea-b37) with Java HotSpot(TM) Server VM (build 14.0-b05, mixed mode).</div>
<div>&nbsp;</div>
<div style="text-align: left;">We hope you find this contribution valuable, and we want to hear from you about whether it makes a difference in your work. Does this optimization help you?</div>
</div>]]></description>
	</item>
	
	<item>
		<dc:creator>Ben Pollan</dc:creator>
		<title>How to Make Sure That Benchmarks Aren&apos;t a Horror Story For You</title>
		<link>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=98674</link> 
		<pubDate>2008-08-13T17:04:27 -05.00</pubDate>
		<comments>http://forums.amd.com/devblog/blogpost.cfm?catid=313&amp;threadid=98674#comments</comments>
		<trackback:ping>0</trackback:ping>
		<description><![CDATA[ <p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: #1f497d;"><span style="font-size: small; font-family: Calibri;">When one of your main jobs is to optimize Java runtimes, it&rsquo;s easy to lose perspective and let benchmark results take over your life.&nbsp; For example, the other day I had a nightmare where I was being chased by the demonic incarnations of thousands of &nbsp;</span><a href="http://www.spec.org/jbb2005/docs/FAQ.html#Q5"><span style="font-size: small; font-family: Calibri;">SPECjbb2005 bops</span></a><span style="font-size: small;"><span style="font-family: Calibri;">.&nbsp; Thankfully, due to a mix of eastern and western medicines this hasn&rsquo;t happened again, although I&rsquo;m thinking of selling the rights to Stephen King.&nbsp; This does bring up an important point:&nbsp; Whatever your involvement with Java benchmarks (any benchmarks, really), you do need to keep them in perspective.</span></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: #1f497d;"><span style="font-size: small; font-family: Calibri;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: #1f497d;"><span style="font-size: small; font-family: Calibri;">AMD is a strong supporter of (and contributor to) benchmarking standards via organizations like </span><a href="http://www.spec.org/"><span style="font-size: small; color: #800080; font-family: Calibri;">SPEC</span></a><span style="font-size: small;"><span style="font-family: Calibri;">.&nbsp; The goal is to come up with workloads that are representative of applications, hopefully your applications, so that you can use the results as data points in software and hardware purchasing decisions.&nbsp; Having said that, we also realize that benchmark results taken out of context won&rsquo;t help you make those decisions.&nbsp; </span></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: #1f497d;"><span style="font-size: small; font-family: Calibri;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: #1f497d;"><span style="font-size: small; font-family: Calibri;">Pat Moorhead, our V.P. of Advanced Marketing, said it well in his </span><a href="http://blogs.amd.com/patmoorhead/archive/2008/08/04/there-are-benchmarks-and-there-are-benchmarks&hellip;.aspx"><span style="font-size: small; color: #800080; font-family: Calibri;">blog</span></a><span style="font-size: small;"><span style="font-family: Calibri;">.&nbsp; Benchmarks like SPECjbb2005 can be configured to run in a number of different ways.&nbsp; Some configurations have the isolated goal of yielding the best possible performance, while others look to simulate the realities of the data center where power consumption, performance, price, and stability are all important.&nbsp; You&rsquo;ve got to look at the context that makes the most sense for your situation, to help you interpret the results.</span></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: #1f497d;"><span style="font-size: small; font-family: Calibri;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: #1f497d;"><span style="font-size: small;"><span style="font-family: Calibri;">Essentially, benchmark results tell a story.&nbsp; You have to make sure that they fit your story, or there be nightmares in your future.</span></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: #1f497d;"><span style="font-size: small; font-family: Calibri;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="font-size: small;"><span style="font-family: Calibri;"><span style="color: #1f497d;">Ben</span></span></span></p>]]></description>
	</item>
	
</channel>
</rss>
