Congratulations!

[Valid Atom 1.0] This is a valid Atom 1.0 feed.

Recommendations

This feed is valid, but interoperability with the widest range of feed readers could be improved by implementing the following recommendations.

Source: http://samueldotj.com/blog/?feed=atom

  1. <?xml version="1.0" encoding="UTF-8"?><feed
  2. xmlns="http://www.w3.org/2005/Atom"
  3. xmlns:thr="http://purl.org/syndication/thread/1.0"
  4. xml:lang="en-US"
  5. >
  6. <title type="text">Samuel Jacob&#039;s Weblog</title>
  7. <subtitle type="text">Just another technical blog</subtitle>
  8.  
  9. <updated>2015-09-12T04:51:24Z</updated>
  10.  
  11. <link rel="alternate" type="text/html" href="http://samueldotj.com/blog" />
  12. <id>http://samueldotj.com/blog/feed/atom/</id>
  13. <link rel="self" type="application/atom+xml" href="http://samueldotj.com/blog/feed/atom/" />
  14.  
  15. <generator uri="https://wordpress.org/" version="6.5.3">WordPress</generator>
  16. <entry>
  17. <author>
  18. <name>samueldotj</name>
  19. </author>
  20.  
  21. <title type="html"><![CDATA[SWIG and Complex C structures]]></title>
  22. <link rel="alternate" type="text/html" href="http://samueldotj.com/blog/swig-and-complex-c-structures/" />
  23.  
  24. <id>http://samueldotj.com/blog/?p=231</id>
  25. <updated>2015-09-12T04:51:24Z</updated>
  26. <published>2015-09-12T04:51:24Z</published>
  27. <category scheme="http://samueldotj.com/blog" term="C" /><category scheme="http://samueldotj.com/blog" term="Programming" />
  28. <summary type="html"><![CDATA[I had to use SWIG to access a kernel module&#8217;s chardev interface through python and found SWIG examples are not enough, so adding my own. Lets take the following example header file. I will explain how to access all the members in complex_struct_t from python. Also extend these structures so that python code would look [&#8230;]]]></summary>
  29.  
  30. <content type="html" xml:base="http://samueldotj.com/blog/swig-and-complex-c-structures/"><![CDATA[<p>I had to use SWIG to access a kernel module&#8217;s chardev interface through python and found SWIG examples are not enough, so adding my own.</p>
  31. <p>Lets take the following example header file.<br />
  32. I will explain how to access all the members in <em>complex_struct_t</em> from python.<br />
  33. Also extend these structures so that python code would look little better.<br />
  34. [codegroup]<br />
  35. [c tab=&#8221;header&#8221;]</p>
  36. <p>/* test.h */</p>
  37. <p>#ifndef _TEST_STRUCT_H<br />
  38. #define _TEST_STRUCT_H</p>
  39. <p>/* a simple structure &#8211; no problem with SWIG */<br />
  40. typedef struct simple_struct {<br />
  41.   int int_var;<br />
  42.   long long_var;<br />
  43.   float float_var;<br />
  44. } simple_struct_t;</p>
  45. <p>typedef struct tlv_base {<br />
  46.   int type;<br />
  47.   int length;<br />
  48.   unsigned char value[];<br />
  49. } tlv_base_t;</p>
  50. <p>typedef struct tlv_type1 {<br />
  51.   tlv_base_t base;<br />
  52.   int stat;<br />
  53.   int info;<br />
  54.   long something;<br />
  55. } tlv_type1_t;</p>
  56. <p>/* relatively complex C structure. */<br />
  57. typedef struct complex_struct {<br />
  58.   char string[10];           //SWIG considers this as null terminated string<br />
  59.   unsigned char bytes[10];   //SWIG wont considers this as string</p>
  60. <p>   simple_struct_t embedded;</p>
  61. <p>   int pointer_array_count;<br />
  62.   simple_struct_t *pointer_array; //SWIG supports only accessing first element.</p>
  63. <p>   tlv_base_t tlv;   //How do cast this to derived_struct_1 ?<br />
  64. } complex_struct_t;</p>
  65. <p>complex_struct_t * alloc_complex_struct(int array_count);<br />
  66. void free_complex_struct(complex_struct_t *cs);<br />
  67. void print_complex_struct(complex_struct_t *cs);</p>
  68. <p>#endif<br />
  69. [/c]</p>
  70. <p>[c tab=&#8217;source&#8217;]</p>
  71. <p>/* test.c */</p>
  72. <p>#include <stdlib.h><br />
  73. #include <stdio.h></p>
  74. <p>#include &#8220;test.h&#8221;</p>
  75. <p>complex_struct_t *<br />
  76. alloc_complex_struct(int array_count)<br />
  77. {<br />
  78.   complex_struct_t *result;<br />
  79.   size_t size;</p>
  80. <p>   result = (complex_struct_t *)malloc(sizeof(complex_struct_t) + sizeof(tlv_type1_t));<br />
  81.   if (result == NULL){<br />
  82.      return NULL;<br />
  83.   }</p>
  84. <p>   result->tlv.type = 1;<br />
  85.   result->tlv.length = sizeof(tlv_type1_t);</p>
  86. <p>   size = sizeof(simple_struct_t) * array_count;<br />
  87.   result->pointer_array = (simple_struct_t *)malloc(size);<br />
  88.   if (result->pointer_array == NULL) {<br />
  89.      free(result);<br />
  90.      return NULL;<br />
  91.   }<br />
  92.   memset(result->pointer_array, 0, size);<br />
  93.   result->pointer_array_count = array_count;</p>
  94. <p>   return result;<br />
  95. }</p>
  96. <p>void<br />
  97. free_complex_struct(complex_struct_t *cs)<br />
  98. {<br />
  99.   free(cs->pointer_array);<br />
  100.   free(cs);<br />
  101. }</p>
  102. <p>static inline void<br />
  103. print_simple_struct(simple_struct_t *ss)<br />
  104. {<br />
  105.   printf(&#8220;int %d long %ld float %f\n&#8221;, ss->int_var, ss->long_var, ss->float_var);<br />
  106. }</p>
  107. <p>void<br />
  108. print_complex_struct(complex_struct_t *cs)<br />
  109. {<br />
  110.   int i;</p>
  111. <p>   printf(&#8220;String = %s\n&#8221;, cs->string);<br />
  112.   printf(&#8220;Embedded : &#8220;);<br />
  113.   print_simple_struct(&#038;cs->embedded);<br />
  114.   printf(&#8220;External : \n&#8221;);<br />
  115.   for (i=0; i<cs->pointer_array_count; i++) {<br />
  116.      printf(&#8220;%d) &#8220;, i + 1);<br />
  117.      print_simple_struct(&#038;cs->pointer_array[i]);<br />
  118.   }<br />
  119. }</p>
  120. <p>[/c]</p>
  121. <p>[raw tab=&#8217;interface&#8217;]<br />
  122. // test_swig.i<br />
  123. %module test_struct<br />
  124. %{<br />
  125.   #include &#8220;../test.h&#8221;<br />
  126. %}</p>
  127. <p>%include &#8220;test.h&#8221;<br />
  128. [/raw]</p>
  129. <p>[shell tab=&#8217;commands&#8217;]<br />
  130. # Commands to make the shared library<br />
  131. $ mkdir -p _build<br />
  132. $ gcc -I /usr/include/python2.7/ -fPIC -c -o _build/test.o test.c<br />
  133. $ swig -python -outdir _build -o _build/test_swig_wrap.c test_swig.i<br />
  134. $ gcc -I /usr/include/python2.7/ -fPIC -c -o _build/test_swig_wrap.o _build/test_swig_wrap.c<br />
  135. $ ld -shared  _build/test.o  _build/test_swig_wrap.o -o _build/_test_struct.so<br />
  136. $ rm _build/test_swig_wrap.c<br />
  137. [/shell]</p>
  138. <p>[raw tab=&#8217;makefile&#8217;]<br />
  139. #Makefile<br />
  140. BLD_DIRECTORY = _build</p>
  141. <p>SWIG_SRCS = test_swig.i<br />
  142. C_SRCS = test.c<br />
  143. CFLAGS = -I /usr/include/python2.7/ -fPIC -c -Wall</p>
  144. <p>OBJS = $(patsubst %.c, $(BLD_DIRECTORY)/%.o, $(C_SRCS)) \<br />
  145. $(patsubst %.i, $(BLD_DIRECTORY)/%_wrap.o, $(SWIG_SRCS))</p>
  146. <p>$(BLD_DIRECTORY)/%.o: %.c %.h<br />
  147. gcc $(CFLAGS)  -o $@ $&lt;</p>
  148. <p>$(BLD_DIRECTORY)/%.o: $(BLD_DIRECTORY)/%.c<br />
  149. gcc $(CFLAGS) -o $@ $&lt;</p>
  150. <p>$(BLD_DIRECTORY)/%_wrap.c: %.i<br />
  151. swig -python -outdir $(BLD_DIRECTORY) -o $@ $&lt;<br />
  152. cp $@ $@.bak</p>
  153. <p>$(BLD_DIRECTORY):<br />
  154. mkdir -p $(BLD_DIRECTORY)</p>
  155. <p>clean:<br />
  156. rm -rf $(BLD_DIRECTORY)</p>
  157. <p>all: $(BLD_DIRECTORY) $(OBJS) $(C_SRCS) $(SWIG_SRCS)<br />
  158. ld -shared $(OBJS) -o $(BLD_DIRECTORY)/_test_struct.so</p>
  159. <p>.PHONY: all clean</p>
  160. <p>.DEFAULT_GOAL := all<br />
  161. [/raw]<br />
  162. [/codegroup]</p>
  163. <p>With this simple interface file, SWIG would be able to create a _test_struct.so and test_struct.py which is perfect for most cases.<br />
  164. [raw tab=&#8217;interface&#8217;]<br />
  165. %module test_struct<br />
  166. %{<br />
  167.   #include &#8220;../test.h&#8221;<br />
  168. %}</p>
  169. <p>%include &#8220;test.h&#8221;<br />
  170. [/raw]</p>
  171. <p>[python]<br />
  172. import test_struct as ts<br />
  173. cs = ts.alloc_complex_struct(1)<br />
  174. cs.string = &#8220;Hello&#8221;<br />
  175. cs.embedded.int_var = 9<br />
  176. cs.embedded.long_var = 10<br />
  177. cs.embedded.float_var = 11.23<br />
  178. ts.print_complex_struct(cs)<br />
  179. ts.free_complex_struct(cs)<br />
  180. [/python]</p>
  181. <p>This shows SWIG&#8217;s ability to convert C string to python string and vice versa.<br />
  182. Similarly accessing primitive structure members is very easy.<br />
  183. Here is the output of the program when ran from _build directory. </p>
  184. <p>[raw]<br />
  185. String = Hello<br />
  186. Embedded : int 9 long 10 float 11.230000<br />
  187. Pointer Array :<br />
  188. 1) int 0 long 0 float 0.000000<br />
  189. [/raw]</p>
  190. <p>It also shows how to call function call into C library. If you have noticed this program looks more like a C program rather than a python program &#8211; mainly because it manages the memory allocation/free. Python can informed that [c] alloc_complex_struct() [/c] returns a new object and it is the responsibility of the caller to free it by using the SWIG typemap [python] newobject [/python]. Now python garbage collector will free the object when there is no reference. But python does not know how to free the object(complex_struct_t) &#8211; this can be done by using [python]newfree[/python] typemap.</p>
  191. <p>By adding the following to the test_swig.i, we can avoid calling free_complex_struct() in python program.<br />
  192. [c]<br />
  193. %typemap(newobject) alloc_complex_struct;<br />
  194. %typemap(newfree) complex_struct_t * {<br />
  195.  free_complex_struct($1);<br />
  196. }<br />
  197. [/c]</p>
  198. <p>Lets modify the program a little bit and access the pointer_array elements.</p>
  199. <p>[python]<br />
  200. import test_struct as ts<br />
  201. cs = ts.alloc_complex_struct(5)<br />
  202. print &#8216;Pointer array count &#8216;, cs.pointer_array_count<br />
  203. print cs.pointer_array[0]<br />
  204. ts.free_complex_struct(cs)<br />
  205. [/python]</p>
  206. <p>This will fail with the following error:<br />
  207. [raw]<br />
  208. Pointer array count  5<br />
  209. Traceback (most recent call last):<br />
  210.  File &#8220;./test.py&#8221;, line 4, in <module><br />
  211.    print cs.pointer_array[0]<br />
  212. TypeError: &#8216;simple_struct_t&#8217; object does not support indexing<br />
  213. [/raw]</p>
  214. <p>The reason is SWIG does not really know [c]simple_struct_t *pointer_array;[/c] actually points to an array of [c]simple_struct_t[/c]. In other words SWIG safely assumes it points to a single entry. If pointer_array was &#8220;array of simple_struct_t pointers&#8221; then <a href="http://www.swig.org/Doc3.0/SWIGDocumentation.html#Library_carrays" target="_blank">carrays.i</a> macro would have been helped. But pointer_array is actually &#8220;pointer to array of simple_struct_t&#8221; so carrays.i won&#8217;t help.</p>
  215. <p>The easiest way is extending complex_struct_t and add a new member(kind of) function to it.<br />
  216. [c]<br />
  217. %extend complex_struct_t{<br />
  218.    simple_struct_t *get_array_element(int i) {<br />
  219.        return &#038;$self->pointer_array[i];<br />
  220.    }<br />
  221. }<br />
  222. [/c]</p>
  223. <p>This way cs.get_array_element(4) will return 4th element in the array.<br />
  224. Similarly tlv elements can be accessed also &#8211; but this time I decided to override indexing operator([]).</p>
  225. <p>[c]<br />
  226. %extend complex_struct_t{<br />
  227.    unsigned char __getitem__(int i) {<br />
  228.        return $self->tlv[i];<br />
  229.    }<br />
  230. }<br />
  231. [/c]</p>
  232. <p>However this is not useful since python cant cast from [c](struct tlv_base *)[/c] to [c]struct tlv_type1 *[/c]. To cast, a C function can be coded or SWIG&#8217;s <em>cpointer.i</em> can be used.</p>
  233. <p>Here is the full test_swig.i<br />
  234. [c]<br />
  235. %module test_struct<br />
  236. %{<br />
  237.   #include &#8220;../test.h&#8221;<br />
  238. %}                                                                                                                                                                       </p>
  239. <p>%include &#8220;test.h&#8221;                                                                                                                                                        </p>
  240. <p>%extend complex_struct{<br />
  241.    simple_struct_t *get_array_element(int i) {<br />
  242.        return &#038;$self->pointer_array[i];<br />
  243.    }<br />
  244. }                                                                                                                                                                        </p>
  245. <p>%typemap(newobject) alloc_complex_struct;<br />
  246. %typemap(newfree) complex_struct_t * {<br />
  247.      free_complex_struct($1);<br />
  248. }                                                                                                                                                                        </p>
  249. <p>%include <cpointer.i><br />
  250. %pointer_cast(tlv_base_t *,  tlv_type1_t *, cast_to_tlv_type1);<br />
  251. [/c]</p>
  252. <p>And test code:<br />
  253. [python]<br />
  254. import test_struct as ts<br />
  255. cs = ts.alloc_complex_struct(5)<br />
  256. cs.string = &#8216;Hello&#8217;<br />
  257. print &#8216;Pointer array count &#8216;, cs.pointer_array_count<br />
  258. for i in range(cs.pointer_array_count):<br />
  259.   simple_struct = cs.get_array_element(i)<br />
  260.   simple_struct.int_var = i * 10<br />
  261.   simple_struct.long_var = i * 20<br />
  262.   simple_struct.float_var = i * 3.3<br />
  263. ts.print_complex_struct(cs)                                                                                                                                              </p>
  264. <p>tlv = ts.cast_to_tlv_type1(cs.tlv)<br />
  265. print tlv.stat, tlv.info, tlv.something<br />
  266. [/python]</p>
  267. ]]></content>
  268. </entry>
  269. <entry>
  270. <author>
  271. <name>samueldotj</name>
  272. </author>
  273.  
  274. <title type="html"><![CDATA[I2C 20&#215;4 LCD module and Arduino]]></title>
  275. <link rel="alternate" type="text/html" href="http://samueldotj.com/blog/i2c-20x4-lcd-module-and-arduino/" />
  276.  
  277. <id>http://samueldotj.com/blog/?p=204</id>
  278. <updated>2013-09-08T23:12:24Z</updated>
  279. <published>2013-09-08T23:12:01Z</published>
  280. <category scheme="http://samueldotj.com/blog" term="Uncategorized" /><category scheme="http://samueldotj.com/blog" term="Arduino" />
  281. <summary type="html"><![CDATA[Recently I was working on a ZigBee project using XBee. Since XBee occupied the Arduino UART port, I decided use a character LCD for debug logs. I got the SainSmart 20&#215;4 LCD module with i2c interface. The connections are simple: Arduino LCD 5v VCC GND GND SDA A5/SDA SCL A4/SCL But I couldnt make it [&#8230;]]]></summary>
  282.  
  283. <content type="html" xml:base="http://samueldotj.com/blog/i2c-20x4-lcd-module-and-arduino/"><![CDATA[<p>Recently I was working on a ZigBee project using XBee. Since XBee occupied the Arduino UART port, I decided use a character LCD for debug logs. I got the <a href="http://www.sainsmart.com/sainsmart-iic-i2c-twi-serial-2004-20x4-lcd-module-shield-for-arduino-uno-mega-r3.html">SainSmart 20&#215;4 LCD module</a> with i2c interface. The connections are simple: </p>
  284. <table class="gridtable">
  285. <tr>
  286. <th>Arduino</th>
  287. <th>LCD</th>
  288. </tr>
  289. <tr>
  290. <td>5v</td>
  291. <td>VCC</td>
  292. </tr>
  293. <tr>
  294. <td>GND</td>
  295. <td>GND</td>
  296. </tr>
  297. <tr>
  298. <td>SDA</td>
  299. <td>A5/SDA</td>
  300. </tr>
  301. <tr>
  302. <td>SCL</td>
  303. <td>A4/SCL</td>
  304. </tr>
  305. </table>
  306. <p>But I couldnt make it work with sample code provided, after few googling I found that the sample code has a wrong i2c address. Even after using the correct I2C address(0x3f), it didnt work, but I was getting only to horizontal black bars on the screen. </p>
  307. <p>I confirmed the I2C is working by using I2C Explorer. Finally after updating the i2c <a href="https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home">library</a> and with the following code, it started working. I found this code on a amazon review and modified for my purpose.</p>
  308. <script src="https://gist.github.com/6489019.js"></script><noscript><pre><code class="language-c c">/*
  309. * Sample code for SainSmart IIC/I2C/TWI Serial 2004 20x4 LCD Module.
  310. * License: GPL
  311. */
  312.  
  313. #include &lt;Wire.h&gt;
  314. #include &lt;LiquidCrystal_I2C.h&gt;
  315.  
  316. #define I2C_ADDR 0x3F
  317. #define En_pin 2
  318. #define Rw_pin 1
  319. #define Rs_pin 0
  320. #define D4_pin 4
  321. #define D5_pin 5
  322. #define D6_pin 6
  323. #define D7_pin 7
  324. #define BACKLIGHT_PIN   3
  325.  
  326. LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin,
  327.                      D4_pin, D5_pin, D6_pin, D7_pin);
  328.  
  329. void setup() {
  330. lcd.begin (20, 4);
  331.  
  332. // Switch on the backlight
  333. lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  334. lcd.setBacklight(LOW);
  335.  
  336. // Position cursor and write some text
  337. lcd.home();
  338. lcd.print(&quot;Hello World&quot;);
  339. }
  340.  
  341. void loop() {
  342. }
  343. </code></pre></noscript>
  344. <p><a href="http://samueldotj.com/blog/wp-content/uploads/2013/09/LCD2004.jpg"><img fetchpriority="high" decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2013/09/LCD2004-300x200.jpg" alt="LCD2004" width="300" height="200" class="alignnone size-medium wp-image-216" srcset="http://samueldotj.com/blog/wp-content/uploads/2013/09/LCD2004-300x200.jpg 300w, http://samueldotj.com/blog/wp-content/uploads/2013/09/LCD2004-1024x682.jpg 1024w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
  345. ]]></content>
  346. </entry>
  347. <entry>
  348. <author>
  349. <name>samueldotj</name>
  350. </author>
  351.  
  352. <title type="html"><![CDATA[Fix loop in a linked list]]></title>
  353. <link rel="alternate" type="text/html" href="http://samueldotj.com/blog/fix-loop-in-a-linked-list/" />
  354.  
  355. <id>http://samueldotj.com/blog/?p=134</id>
  356. <updated>2013-08-29T22:35:33Z</updated>
  357. <published>2013-08-29T22:35:33Z</published>
  358. <category scheme="http://samueldotj.com/blog" term="Uncategorized" />
  359. <summary type="html"><![CDATA[Cycle or loop in a linked list with nodes is shown in pictorial representation. In this picture the number of nodes in the list is and the last node points to node at a position x(which created the cycle). From the picture it is obvious that to fix the loop: Find the last element Correct [&#8230;]]]></summary>
  360.  
  361. <content type="html" xml:base="http://samueldotj.com/blog/fix-loop-in-a-linked-list/"><![CDATA[<p>Cycle or loop in a linked list with <img decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-9f9c792f3d1c0d436f1e9ae9ff2e4949_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#110;" title="Rendered by QuickLaTeX.com" height="7" width="10" style="vertical-align: 0px;"/> nodes is shown in pictorial representation.<br />
  362. <div id="attachment_138" style="width: 310px" class="wp-caption alignnone"><a href="http://samueldotj.com/blog/wp-content/uploads/2013/08/Cycle.png"><img decoding="async" aria-describedby="caption-attachment-138" src="http://samueldotj.com/blog/wp-content/uploads/2013/08/Cycle-300x65.png" alt="Loop in a singly linked list" width="300" height="65" class="size-medium wp-image-138" srcset="http://samueldotj.com/blog/wp-content/uploads/2013/08/Cycle-300x65.png 300w, http://samueldotj.com/blog/wp-content/uploads/2013/08/Cycle.png 583w" sizes="(max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-138" class="wp-caption-text">Loop in a singly linked list</p></div></p>
  363. <p>In this picture the number of nodes in the list is <img decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-9f9c792f3d1c0d436f1e9ae9ff2e4949_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#110;" title="Rendered by QuickLaTeX.com" height="7" width="10" style="vertical-align: 0px;"/> and the last node points to node at a position x(which created the cycle). From the picture it is obvious that to fix the loop:</p>
  364. <ol>
  365. <li>Find the last element</li>
  366. <li>Correct it to point end(NULL)</li>
  367. </ol>
  368. <p>In the above steps, step 1(find the last element) is little difficult if total number of nodes in the list is <strong>unknown</strong>. In this post I will describe a method I discovered to find the last node. I drew couple of pictures to explain the complexity of the problem. The following pictures explains the same loop condition but in different form. It explains why it is hard to find the last node.<br />
  369. <a href="http://samueldotj.com/blog/wp-content/uploads/2013/08/Cycle2.png"><img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2013/08/Cycle2.png" alt="Cycle2" width="635" height="91" class="alignnone size-full wp-image-151" srcset="http://samueldotj.com/blog/wp-content/uploads/2013/08/Cycle2.png 635w, http://samueldotj.com/blog/wp-content/uploads/2013/08/Cycle2-300x42.png 300w" sizes="(max-width: 635px) 100vw, 635px" /></a></p>
  370. <p><a href="http://samueldotj.com/blog/wp-content/uploads/2013/08/Cycle3.png"><img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2013/08/Cycle3.png" alt="Cycle3" width="469" height="108" class="alignnone size-full wp-image-152" srcset="http://samueldotj.com/blog/wp-content/uploads/2013/08/Cycle3.png 469w, http://samueldotj.com/blog/wp-content/uploads/2013/08/Cycle3-300x69.png 300w" sizes="(max-width: 469px) 100vw, 469px" /></a><br />
  371. The illustration also tells us the following<br />
  372. <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-9b30a924495e1f93eff66310347f2ac0_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#110;&#32;&#61;&#32;&#120;&#43;&#121;" title="Rendered by QuickLaTeX.com" height="14" width="71" style="vertical-align: -3px;"/><br />
  373. where<br />
  374. &nbsp; <img decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-9f9c792f3d1c0d436f1e9ae9ff2e4949_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#110;" title="Rendered by QuickLaTeX.com" height="7" width="10" style="vertical-align: 0px;"/> is total number of nodes in the list<br />
  375. &nbsp; <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-8aa17fa6615962eb1a747b73bcc92f80_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#120;" title="Rendered by QuickLaTeX.com" height="7" width="9" style="vertical-align: 0px;"/> is number of nodes before the cycle starts.<br />
  376. &nbsp; <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-5f08a280df35506f9363e0d1703cd851_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#121;" title="Rendered by QuickLaTeX.com" height="10" width="9" style="vertical-align: -3px;"/> is number of nodes forming the cycle.</p>
  377. <p>If we have value of <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-8aa17fa6615962eb1a747b73bcc92f80_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#120;" title="Rendered by QuickLaTeX.com" height="7" width="9" style="vertical-align: 0px;"/> and <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-5f08a280df35506f9363e0d1703cd851_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#121;" title="Rendered by QuickLaTeX.com" height="10" width="9" style="vertical-align: -3px;"/> then the equation can be solved, which will give the last node’s location.</p>
  378. <p><strong>Finding <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-5f08a280df35506f9363e0d1703cd851_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#121;" title="Rendered by QuickLaTeX.com" height="10" width="9" style="vertical-align: -3px;"/></strong></p>
  379. <ol>
  380. <li>Detect the cycle using Floyd’s Cycle detection algorithm. (Two pointer with varying speed)</li>
  381. <li>When cycle is detected
  382. <ol>
  383. <li>Store the current node as H</li>
  384. <li>Move to next node and increment y(y++)</li>
  385. <li>If current node is H then exit</li>
  386. <li>Goto step 2</li>
  387. </ol>
  388. </li>
  389. </ol>
  390. <p><strong>Finding <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-8aa17fa6615962eb1a747b73bcc92f80_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#120;" title="Rendered by QuickLaTeX.com" height="7" width="9" style="vertical-align: 0px;"/></strong><br />
  391. Let’s first see the easiest way to solve this:</p>
  392. <ol>
  393. <li>Have two pointers (p1, p2)</li>
  394. <li>p1 = node 0</li>
  395. <li>p2 = p1 + y nodes</li>
  396. <li>if p1 == p2 then we found the result is <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-6f2ecc48c4d5bf5138ee701167a61b2d_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#120;&#32;&#61;&#32;&#112;&#49;" title="Rendered by QuickLaTeX.com" height="15" width="47" style="vertical-align: -3px;"/></li>
  397. <li>p1 = p1 + y nodes</li>
  398. <li>goto step 3.</li>
  399. </ol>
  400. <p>The complexity of this algorithm linear depends on <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-8aa17fa6615962eb1a747b73bcc92f80_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#120;" title="Rendered by QuickLaTeX.com" height="7" width="9" style="vertical-align: 0px;"/>. For example if <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-8aa17fa6615962eb1a747b73bcc92f80_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#120;" title="Rendered by QuickLaTeX.com" height="7" width="9" style="vertical-align: 0px;"/> is 10K and <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-5f08a280df35506f9363e0d1703cd851_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#121;" title="Rendered by QuickLaTeX.com" height="10" width="9" style="vertical-align: -3px;"/> is 2 then number of node visits are > 20K. In other words the complexity is <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-be009ed84c5dbef8c9da4e0ab80612af_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#79;&#40;&#50;&#120;&#32;&#43;&#32;&#121;&#41;" title="Rendered by QuickLaTeX.com" height="18" width="72" style="vertical-align: -4px;"/>.</p>
  401. <p>I believe it can be done in <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-f17985b51fa15d9e24ccb364fde3fe90_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#79;&#40;&#110;&#41;" title="Rendered by QuickLaTeX.com" height="18" width="34" style="vertical-align: -4px;"/> or <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-5a4d08945942ad313f7a743439496291_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#79;&#40;&#120;&#32;&#43;&#32;&#121;&#41;" title="Rendered by QuickLaTeX.com" height="18" width="63" style="vertical-align: -4px;"/> with following method This following diagram captures state when the &#8220;tortoise and the hare&#8221; algorithm detects the cycle.<br />
  402. <a href="http://samueldotj.com/blog/wp-content/uploads/2013/08/CycleFix1.png"><img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2013/08/CycleFix1.png" alt="CycleFix" width="506" height="130" class="alignnone size-full wp-image-159" srcset="http://samueldotj.com/blog/wp-content/uploads/2013/08/CycleFix1.png 506w, http://samueldotj.com/blog/wp-content/uploads/2013/08/CycleFix1-300x77.png 300w" sizes="(max-width: 506px) 100vw, 506px" /></a></p>
  403. <p>Lets define few more variables at the time of cycle detection:<br />
  404. <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-2f2cb00cf96aa920ab1041073bf8ad6a_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#84;" title="Rendered by QuickLaTeX.com" height="11" width="12" style="vertical-align: 0px;"/> is the total number of nodes tortoise traveled.<br />
  405. <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-a6f2b49d15c8a555a5b861c560881b92_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#72;" title="Rendered by QuickLaTeX.com" height="11" width="15" style="vertical-align: 0px;"/> is the total number of nodes hare traveled.<br />
  406. <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-2650c16c3c9e38aa51f9867a23890025_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#114;" title="Rendered by QuickLaTeX.com" height="7" width="8" style="vertical-align: 0px;"/> can be defined as number of nodes before the &#8216;last node&#8217;</p>
  407. <p><img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-c70da08357ecd9bf6af83d7b204b158d_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#84;&#32;&#61;&#32;&#50;&#72;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101; &#120;&#32;&#43;&#32;&#99;&#121;&#32;&#45;&#32;&#114;&#32;&#61;&#32;&#84;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101; &#120;&#32;&#43;&#32;&#121;&#32;&#45;&#32;&#114;&#32;&#61;&#32;&#72;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101;" title="Rendered by QuickLaTeX.com" height="56" width="108" style="vertical-align: 18px;"/><br />
  408. where c is the number of complete cycles hare covered.<br />
  409. <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-e517549f49202880dd0ee979796fe948_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#84;&#32;&#62;&#32;&#72;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101; &#99;&#32;&#62;&#32;&#49;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101; &#114;&#32;&#62;&#61;&#32;&#120;&#32;&#92;&#32;&#97;&#110;&#100;&#32;&#92;&#32;&#114;&#32;&#60;&#61;&#32;&#110;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101;" title="Rendered by QuickLaTeX.com" height="52" width="144" style="vertical-align: 21px;"/><br />
  410. From this we can derive<br />
  411. <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-322906623d83aeb0b7cd20ba5f67f2ea_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#99;&#32;&#61;&#32;&#72;&#32;&#47;&#32;&#121;&#32;&#43;&#32;&#49;" title="Rendered by QuickLaTeX.com" height="16" width="88" style="vertical-align: -4px;"/></p>
  412. <p>Using value of <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-9fbeb23be22f99c93f330d0033c50568_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#99;" title="Rendered by QuickLaTeX.com" height="7" width="8" style="vertical-align: 0px;"/>, <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-2650c16c3c9e38aa51f9867a23890025_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#114;" title="Rendered by QuickLaTeX.com" height="7" width="8" style="vertical-align: 0px;"/> can be derived.<br />
  413. <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-cb01f9fc40f4d1767f4ea1db4dd72fc4_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#120;&#32;&#43;&#32;&#121;&#32;&#45;&#32;&#114;&#32;&#61;&#32;&#72;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101; &#114;&#32;&#61;&#32;&#120;&#32;&#43;&#32;&#121;&#32;&#45;&#32;&#72;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101; &#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101; &#120;&#32;&#43;&#32;&#99;&#121;&#32;&#45;&#32;&#114;&#32;&#61;&#32;&#84;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101; &#120;&#32;&#61;&#32;&#32;&#84;&#32;&#45;&#32;&#99;&#121;&#32;&#43;&#32;&#114;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101; &#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101; &#114;&#32;&#61;&#32;&#32;&#84;&#32;&#45;&#32;&#99;&#121;&#32;&#43;&#32;&#114;&#32;&#32;&#43;&#32;&#32;&#121;&#32;&#45;&#32;&#72;&#32;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101; &#114;&#32;&#61;&#32;&#32;&#72;&#32;&#45;&#32;&#99;&#121;&#32;&#43;&#32;&#114;&#32;&#32;&#43;&#32;&#32;&#121;&#32;&#32;&#32;&#32;&#32;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101;" title="Rendered by QuickLaTeX.com" height="159" width="171" style="vertical-align: 18px;"/></p>
  414. <p>Using this formula <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-2650c16c3c9e38aa51f9867a23890025_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#114;" title="Rendered by QuickLaTeX.com" height="7" width="8" style="vertical-align: 0px;"/> can be found. With this <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-2650c16c3c9e38aa51f9867a23890025_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#114;" title="Rendered by QuickLaTeX.com" height="7" width="8" style="vertical-align: 0px;"/> value, the last node can be found by travelling <img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/ql-cache/quicklatex.com-0dfe30d9edc6651a2b70a0afb7f6828f_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#114;&#45;&#49;" title="Rendered by QuickLaTeX.com" height="13" width="35" style="vertical-align: -1px;"/> nodes where T and H met.</p>
  415. ]]></content>
  416. </entry>
  417. <entry>
  418. <author>
  419. <name>samueldotj</name>
  420. </author>
  421.  
  422. <title type="html"><![CDATA[LLDB Backtrace formatting]]></title>
  423. <link rel="alternate" type="text/html" href="http://samueldotj.com/blog/lldb-backtrace-formatting/" />
  424.  
  425. <id>http://samueldotj.com/blog/?p=4</id>
  426. <updated>2013-08-26T23:25:45Z</updated>
  427. <published>2013-07-23T04:20:00Z</published>
  428. <category scheme="http://samueldotj.com/blog" term="C" /><category scheme="http://samueldotj.com/blog" term="Debugger" /><category scheme="http://samueldotj.com/blog" term="lldb" />
  429. <summary type="html"><![CDATA[lldb can be configured to print backtraces with syntax highlighting. Here is how to setup lldb to do that Consider the following source level debugging session, [codegroup] [c tab=&#8217;source&#8217;] $ cat test.c static void crash_me() { char *c = 0; *c = 0; } static void recursive_call(int value) { if (value == 0) { crash_me(); [&#8230;]]]></summary>
  430.  
  431. <content type="html" xml:base="http://samueldotj.com/blog/lldb-backtrace-formatting/"><![CDATA[<p>lldb can be configured to print backtraces with syntax highlighting. Here is how to setup lldb to do that</p>
  432. <p>Consider the following source level debugging session,<br />
  433. [codegroup]<br />
  434. [c tab=&#8217;source&#8217;]<br />
  435. $ cat test.c<br />
  436. static void crash_me()<br />
  437. {<br />
  438.    char *c = 0;<br />
  439.    *c = 0;<br />
  440. }</p>
  441. <p>static void recursive_call(int value)<br />
  442. {<br />
  443.    if (value == 0) {<br />
  444.        crash_me();<br />
  445.    }<br />
  446.    recursive_call(value &#8211; 1);<br />
  447. }</p>
  448. <p>int main(int argc, char argv[])<br />
  449. {<br />
  450.    recursive_call(argc);<br />
  451. }<br />
  452. [/c]<br />
  453. [shell tab=&#8217;commands&#8217;]<br />
  454. $ gcc -g3 -O3 test.c<br />
  455. $ lldb a.out<br />
  456. (lldb) run 0 1 2 3 4 5<br />
  457. [/shell]<br />
  458. [/codegroup]</p>
  459. <p>Without color syntax the backtrace would look like the following.<br />
  460. <div id="attachment_127" style="width: 310px" class="wp-caption alignnone"><a href="http://samueldotj.com/blog/wp-content/uploads/2013/07/lldb-nocolor.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-127" src="http://samueldotj.com/blog/wp-content/uploads/2013/07/lldb-nocolor-300x98.png" alt="Normal backtrace with out any makeup" width="300" height="98" class="size-medium wp-image-127" srcset="http://samueldotj.com/blog/wp-content/uploads/2013/07/lldb-nocolor-300x98.png 300w, http://samueldotj.com/blog/wp-content/uploads/2013/07/lldb-nocolor-1024x337.png 1024w, http://samueldotj.com/blog/wp-content/uploads/2013/07/lldb-nocolor.png 1841w" sizes="(max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-127" class="wp-caption-text">Normal backtrace with out any makeup</p></div></p>
  461. <p>Since lldb supports ANSI escape sequence, the escape sequences can be used to color the backtrace output which makes output more readable. Here is the link to official lldb page describing this feature &#8211; <a href='http://lldb.llvm.org/formats.html'>http://lldb.llvm.org/formats.html</a>.</p>
  462. <p>Here is my backtrace setting and example</p>
  463. <p>[shell]<br />
  464. (lldb) settings set frame-format &#8220;frame #${frame.index}: ${frame.pc}{ \x1b\x5b36m${module.file.basename}\x1b\x5b39m{` \x1b\x5b33m${function.name-with-args} \x1b\x5b39m${function.pc-offset}}}{ at ${line.file.basename}:${line.number}}\n&#8221;<br />
  465. [/shell]<br />
  466. <div id="attachment_125" style="width: 310px" class="wp-caption alignnone"><a href="http://samueldotj.com/blog/wp-content/uploads/2013/07/lldb-color.jpg"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-125" src="http://samueldotj.com/blog/wp-content/uploads/2013/07/lldb-color-300x53.jpg" alt="Backtrace with color" width="300" height="53" class="size-medium wp-image-125" srcset="http://samueldotj.com/blog/wp-content/uploads/2013/07/lldb-color-300x53.jpg 300w, http://samueldotj.com/blog/wp-content/uploads/2013/07/lldb-color-1024x181.jpg 1024w, http://samueldotj.com/blog/wp-content/uploads/2013/07/lldb-color.jpg 1841w" sizes="(max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-125" class="wp-caption-text">Backtrace with color</p></div></p>
  467. <p>Similarly thread format cant be colorized so that &#8216;<strong>thread list</strong>&#8216; would look neat.</p>
  468. <p>[shell]<br />
  469. (lldb) settings set thread-format &#8220;\x1b\x5b42;1mthread #${thread.index}: tid = ${thread.id}{, ${frame.pc}}{ \x1b\x5b31m${module.file.basename}\x1b\x5b39m{`${function.name-with-args}${function.pc-offset}}}{ at ${line.file.basename}:${line.number}}{, name = &#8216;\x1b\x5b34m${thread.name}}\x1b\x5b39m{, queue = &#8216;${thread.queue}}{, stop reason = ${thread.stop-reason}}{\nReturn value: ${thread.return-value}}\x1b\x5b0m\n&#8221;<br />
  470. [/shell]</p>
  471. <p>Reference to ANSI escape sequence &#8211; <a href="http://ascii-table.com/ansi-escape-sequences.php">http://ascii-table.com/ansi-escape-sequences.php</a></p>
  472. ]]></content>
  473. </entry>
  474. <entry>
  475. <author>
  476. <name>samueldotj</name>
  477. </author>
  478.  
  479. <title type="html"><![CDATA[Self Balancing Tree as Heap]]></title>
  480. <link rel="alternate" type="text/html" href="http://samueldotj.com/blog/self-balancing-tree-as-heap/" />
  481.  
  482. <id>http://samueldotj.com/blog/?p=5</id>
  483. <updated>2013-08-27T01:57:59Z</updated>
  484. <published>2013-05-11T18:59:00Z</published>
  485. <category scheme="http://samueldotj.com/blog" term="Ace" />
  486. <summary type="html"><![CDATA[Here is my thoughts about how to combine a Heap and AVL tree and get benefit of both them from a single data structure. A self balancing binary search tree such as AVL tree can do faster lookup for a item in the tree in O(log n). Heaps are mainly used to implement priority queues which [&#8230;]]]></summary>
  487.  
  488. <content type="html" xml:base="http://samueldotj.com/blog/self-balancing-tree-as-heap/"><![CDATA[<p>Here is my thoughts about how to combine a <a href='http://en.wikipedia.org/wiki/Heap_(data_structure)'>Heap</a> and <a href='http://en.wikipedia.org/wiki/AVL_tree'>AVL tree</a> and get benefit of both them from a single data structure.</p>
  489. <p>A <a href="http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree">self balancing binary search tree</a> such as <a href="http://en.wikipedia.org/wiki/AVL_tree">AVL tree</a> can do faster lookup for a item in the tree in O(log n). <a href="http://en.wikipedia.org/wiki/Heap_(data_structure)">Heap</a>s are mainly used to implement priority queues which needs to find min/max elements quickly. Heap achieves this in O(1) time complexity.</p>
  490. <p><a href="http://samueldotj.com/blog/wp-content/uploads/2013/05/Balanced-BST11.png"><img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2013/05/Balanced-BST11-300x204.png" alt="Balanced BST1" width="300" height="204" class="alignright size-medium wp-image-57" srcset="http://samueldotj.com/blog/wp-content/uploads/2013/05/Balanced-BST11-300x204.png 300w, http://samueldotj.com/blog/wp-content/uploads/2013/05/Balanced-BST11.png 476w" sizes="(max-width: 300px) 100vw, 300px" /></a> Lets revisit a basic property of binary search tree &#8211; In a binary search tree min element is at the left most leaf position. Similarly in a BST max element is at the right most leaf position. So finding min/max element in a BST is O(h) where h is the depth of the tree. If the BST is a balanced tree then O(h) is O(log n) in worst case(otherwise the worst case O(n), since we considering only balanced trees here lets ignore the unbalanced cases). The following diagram illustrates a basic property of the BST &#8211; min element is always on the left most node.</p>
  491. <p><div id="attachment_64" style="width: 310px" class="wp-caption alignleft"><a href="http://samueldotj.com/blog/wp-content/uploads/2013/05/BST-PointerToMin1.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-64" src="http://samueldotj.com/blog/wp-content/uploads/2013/05/BST-PointerToMin1-300x204.png" alt="Cached pointer to Min element at the root" width="300" height="204" class="size-medium wp-image-64" srcset="http://samueldotj.com/blog/wp-content/uploads/2013/05/BST-PointerToMin1-300x204.png 300w, http://samueldotj.com/blog/wp-content/uploads/2013/05/BST-PointerToMin1.png 473w" sizes="(max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-64" class="wp-caption-text">Cached pointer to Min element at the root</p></div> Using this basic property, min remove operation can be optimized. The first and simplest optimization comes to mind is store a pointer to min/max elements. This caching will result in O(1) time complexity for finding min/max elements. However this would increase the cost of node insert/delete because the min/max pointer has to be updated during insert and deletion. The cost of finding and deleting a min node is O(log(n)) which is same as if we havent had the cache pointers. The picture in the right shows advantage of having cached pointer to find a min element. Obviously this method cant be used for priority queues where find min/delete operation is used together.</p>
  492. <p>In the above method the problem was the complexity finding next smallest element in the tree from min element is O(log n). So if we have pointer to next smallest element from all nodes then find and delete opearation would be of complexity O(1).</p>
  493. <p>Lets look at the BST from slightly different angle. Usual declaration of BST in C as follows:</p>
  494. <p>[c]<br />
  495. struct binary_tree<br />
  496. {<br />
  497.   struct binary_tree *left;<br />
  498.   struct binary_tree *right;<br />
  499. };<br />
  500. [/c]</p>
  501. <p>When we(me and <a href="http://plus.google.com/105622177928011443688" target="_blank">+Dilip Simha</a>) were implementing ADT for AceOS we decided to experiment BST in a different way. We saw tree as recursive lists rather than a recursive pointers. </p>
  502. <p>In the following picture you could see 6 lists(not counting sub-lists):<br />
  503. <a href="http://samueldotj.com/blog/wp-content/uploads/2013/05/Balanced-BST12.png"><img decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2013/05/Balanced-BST12.png" alt="A list is highlighted" class="alignright size-medium wp-image-62" width="220" /></a></p>
  504. <ol>
  505. <li>(300, 200, 100, 50)</li>
  506. <li>(300, 400, 500, 600)</li>
  507. <li>(200, 250, 275)</li>
  508. <li>(100, 150)</li>
  509. <li>(400, 350)</li>
  510. <li>(500, 450)</li>
  511. </ol>
  512. <p>Now consider this list is a doubly linked circular list. This is illustrated in the following figure. You may argue that this will make the BST to become cyclic directed graph. But for the sake of simplicity lets continue to call this as balanced BST. In the picture I left out few arrows to keep it cleaner.</p>
  513. <div id="attachment_63" style="width: 483px" class="wp-caption aligncenter"><a href="http://samueldotj.com/blog/wp-content/uploads/2013/05/Binary-Search-Tree-Doule-Pointer.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-63" src="http://samueldotj.com/blog/wp-content/uploads/2013/05/Binary-Search-Tree-Doule-Pointer.png" alt="Binary Search Tree with nodes having pointer to parent also" width="473" height="324" class="size-full wp-image-63" srcset="http://samueldotj.com/blog/wp-content/uploads/2013/05/Binary-Search-Tree-Doule-Pointer.png 473w, http://samueldotj.com/blog/wp-content/uploads/2013/05/Binary-Search-Tree-Doule-Pointer-300x205.png 300w" sizes="(max-width: 473px) 100vw, 473px" /></a><p id="caption-attachment-63" class="wp-caption-text">Binary Search Tree with nodes having pointer to parent also</p></div>
  514. <p>[c]<br />
  515. typedef struct list LIST, * LIST_PTR;<br />
  516. struct list {<br />
  517.   LIST_PTR next;<br />
  518.   LIST_PTR prev;<br />
  519. };</p>
  520. <p>typedef struct binary_tree<br />
  521. {<br />
  522.   LIST left;<br />
  523.   LIST right;<br />
  524. }BINARY_TREE;</p>
  525. <p>typedef struct avl_tree<br />
  526. {<br />
  527.   int height;    /*! height of the node*/<br />
  528.   BINARY_TREE bintree; /*! low level binary tree*/<br />
  529. }AVL_TREE;<br />
  530. [/c]</p>
  531. <p>AVL tree in Ace OS  is implemented in this way. You can see the data structure declarations below. Initially we did it for reusing the code. But after implementing this we figured out some interesting properties. This balanced tree/graph can find any node in O(log(n)) and also offers <strong>findmin</strong> operation in O(1) complexity. This also reduces the complexity of delete operation(since we can find right node&#8217;s left most child in O(1) operation). But delete operation might result in balancing the tree.</p>
  532. ]]></content>
  533. </entry>
  534. <entry>
  535. <author>
  536. <name>samueldotj</name>
  537. </author>
  538.  
  539. <title type="html"><![CDATA[Internals of GNU Code Coverage &#8211; gcov]]></title>
  540. <link rel="alternate" type="text/html" href="http://samueldotj.com/blog/gcov-internals-overview/" />
  541.  
  542. <id>http://samueldotj.com/blog/?p=6</id>
  543. <updated>2013-09-08T23:18:45Z</updated>
  544. <published>2012-03-31T23:21:00Z</published>
  545. <category scheme="http://samueldotj.com/blog" term="C" /><category scheme="http://samueldotj.com/blog" term="gcc" /><category scheme="http://samueldotj.com/blog" term="Tools" />
  546. <summary type="html"><![CDATA[Few years ago I worked on a small project to extract code coverage information created by gcc from FreeBSD based kernel. During that time I didn&#8217;t find any good internal documentation about gcov. So here I post what I learned. Before jumping to the internals of GCOV here is an example from the man page. [&#8230;]]]></summary>
  547.  
  548. <content type="html" xml:base="http://samueldotj.com/blog/gcov-internals-overview/"><![CDATA[<p>Few years ago I worked on a small project to extract <a href="http://en.wikipedia.org/wiki/Code_coverage">code coverage</a> information created by gcc from FreeBSD based <a href="http://www.freebsd.org/">kernel</a>.  During that time I didn&#8217;t find any good internal documentation about <a href="http://gcc.gnu.org/onlinedocs/gcc/Gcov.html">gcov</a>. So here I post what I learned. Before jumping to the internals of GCOV here is an example from the <a href="http://nixdoc.net/man-pages/openbsd/man1/gcov.1.html">man </a>page.</p>
  549. <pre class="code">
  550. $ gcov -b tmp.c
  551. <span style='color:green'>87.50% of 8 source lines executed in file tmp.c
  552. 80.00% of 5 branches executed in file tmp.c
  553. 80.00% of 5 branches taken  at  least  once  in  file tmp.c
  554. 50.00% of 2 calls executed in file tmp.c</span>
  555. Creating tmp.c.gcov.
  556. Here is a sample of a resulting tmp.c.gcov file:
  557.  
  558.      main()
  559.      {
  560. <span style='color:blue'>    1</span>        int i, total;
  561. <span style='color:blue'>    1</span>        total = 0;
  562. <span style='color:blue'>   11</span>        for (i = 0; i < 10; i++)
  563. <span style='color:green'>branch 0 taken = 91%
  564. branch 1 taken = 100%
  565. branch 2 taken = 100%</span>
  566. <span style='color:blue'>   10</span>        total += i;
  567. <span style='color:blue'>   1</span>         if (total != 45)
  568. <span style='color:green'>branch 0 taken = 100%</span>
  569. <span style='color:red'>  ######</span>          printf ("Failure0);
  570. <span style='color:red'>call 0 never executed
  571. branch 1 never executed</span>
  572.             else
  573. <span style='color:blue'>    1</span>             printf ("Success0);
  574. <span style='color:orange'>call 0 returns = 100%</span>
  575. <span style='color:blue'>    1</span>    }
  576. </pre>
  577. <p>Note &#8211; gcov has a cool graphical front-end in Linux &#8211; <a href="http://ltp.sourceforge.net/coverage/lcov/output/example/example.c.gcov.frameset.html">lcov</a>.<br />
  578. As shown above gcov can show what all code path executed and how many time executed.<br />
  579. Want to try? Here is the quick example.<br />
  580. [shell]<br />
  581. $ gcc -fprofile-arcs -ftest-coverage your_program.c<br />
  582. $ ./a.out<br />
  583. $ gcov your_program.c<br />
  584. [/shell]</p>
  585. <p>During compilation with <a href="http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html">-ftest-coverage</a> option gcc generates a <a href="http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Gcov-Data-Files.html">&#8220;.gcno&#8221;</a> file. It contains information about each branches in your code. While finishing execution, ./a.out creates <b>.gcda</b> file(s) which actually contains which all branches taken(<a href='http://en.wikipedia.org/wiki/Basic_block'>basic block</a> entry/exit). Using these there files .c(source), .gcno(block info) and .gcda(block execution count) gcov command prints the code coverage information in a human readable format.</p>
  586. <p>You might wonder how your ./a.out would create .gcda while exiting the program. It is because of &#8220;-fprofile-arcs&#8221; automatically includes <a href="http://gcc.gnu.org/viewcvs/trunk/libgcc/libgcov.c?view=markup">libgcov</a>. Libgcov registers itself to be invoked during program exit by using <a href="http://pubs.opengroup.org/onlinepubs/009604599/functions/atexit.html">atexit()</a>. (Yes &#8211; it wont generate .gcda files if you exit abnormally). And during program <a href="http://pubs.opengroup.org/onlinepubs/000095399/functions/exit.html">exit</a> it just dumps all the branch information to one or more gcda file.</p>
  587. <p>The coverage information is &#8220;just&#8221; dumped into files by libgcov. So who collects the the coverage information at run time? Actually the program itself collects the coverage information. In-fact only it can collect because only it knew which all code path it takes. The code coverage information is collected at <strong>run-time</strong> on the fly. It is accomplished by having a counter for each branch. For example consider the following program.</p>
  588. <pre class="code">
  589. <span style='color:green'>int if_counter = 0, else_counter = 0;</span>
  590. <span style='color:blue'>
  591. void dump_counters()
  592. {
  593. int fd;
  594. fd = open(strcat(filename, ".gcda"), "w");
  595. write(fd, if_counter, sizeof(if_counter));
  596. write(fd, else_counter, sizeof(else_counter));
  597. }
  598. </span>
  599. int main(int argc, char *argv[])
  600. {
  601. <span style='color:green'>atexit(dump_counters);</span>
  602. if(argc > 1) {
  603. <span style='color:blue'>if_counter++;</span>
  604. printf("Arguments provided\n");
  605. } else {
  606. <span style='color:blue'>else_counter++;</span>
  607. printf("No arguments\n");
  608. }
  609. }
  610. </pre>
  611. <p>If you replace the above example with gcov then green colored code is provided by libgcov(during link/load) and the blue colored coded inserted into your executable by gcc(during compilation).</p>
  612. <p>It is easy to speculate how the increment operation would be be implanted inside your code by gcc. gcc just inserts <b>&#8220;inc <i>x-counter</i>&#8220;</b> machine instruction before and after every branch. It should be noted that &#8220;inc&#8221; is instruction might have side effect on some programs which uses asm inside C. For example in x86 the &#8220;<a href="http://maven.smith.edu/~thiebaut/ArtOfAssembly/CH06/CH06-2.html#HEADING2-117">inc</a>&#8221; instruction affects <a href="http://en.wikibooks.org/wiki/X86_Assembly/X86_Architecture#EFLAGS_Register">carry flag</a>. Some assembly code might depends on this and if gcc inserts &#8220;inc counter&#8221; instruction then it will result in error. I had hard time figuring this out when compiled with -fprofile-arcs a kernel was booting but not able to receive any network packets(it was discarding all packets because the network stack found the <a href="http://www.netfor2.com/checksum.html">checksum</a> was wrong).</p>
  613. <p>Here is a simple C program&#8217;s disassembly:<br />
  614. [codegroup]<br />
  615. [c tab=&#8217;disassembly&#8217;]<br />
  616. int main()<br />
  617. {<br />
  618.  4004b4:       55                      push   %rbp<br />
  619.  4004b5:       48 89 e5                mov    %rsp,%rbp<br />
  620.    int a = 1;<br />
  621.  4004b8:       c7 45 fc 01 00 00 00    movl   $0x1,-0x4(%rbp)</p>
  622. <p>    if (a) {<br />
  623.  4004bf:       83 7d fc 00             cmpl   $0x0,-0x4(%rbp)<br />
  624.  4004c3:       74 06                   je     4004cb <main+0x17><br />
  625.        a++;<br />
  626.  4004c5:       83 45 fc 01             addl   $0x1,-0x4(%rbp)<br />
  627.  4004c9:       eb 04                   jmp    4004cf <main+0x1b><br />
  628.    } else {<br />
  629.        a&#8211;;<br />
  630.  4004cb:       83 6d fc 01             subl   $0x1,-0x4(%rbp)<br />
  631.    }</p>
  632. <p>    return a;<br />
  633.  4004cf:       8b 45 fc                mov    -0x4(%rbp),%eax<br />
  634. }<br />
  635. [/c]</p>
  636. <p>[c tab=&#8217;source&#8217;]<br />
  637. int main()<br />
  638. {<br />
  639.    int a = 1;</p>
  640. <p>    if (a) {<br />
  641.        a++;<br />
  642.    } else {<br />
  643.        a&#8211;;<br />
  644.    }</p>
  645. <p>    return a;<br />
  646. }<br />
  647. [/c]</p>
  648. <p>[shell tab=&#8217;command&#8217;]<br />
  649. gcc -g3 test.c<br />
  650. objdump -S -d ./a.out<br />
  651. [/shell]</p>
  652. <p>[/codegroup]</p>
  653. <p>When the <strong>same program compiled with profile-arcs</strong>, the disassembly looks like</p>
  654. <pre class="code">
  655. int main()
  656. {
  657.  400c34:       55                      push   %rbp
  658.  400c35:       48 89 e5                mov    %rsp,%rbp
  659.  400c38:       48 83 ec 10             sub    $0x10,%rsp
  660.    int a = 1;
  661.  400c3c:       c7 45 fc 01 00 00 00    movl   $0x1,-0x4(%rbp)
  662.  
  663.    if (a) {
  664.  400c43:       83 7d fc 00             cmpl   $0x0,-0x4(%rbp)
  665.  400c47:       74 18                   je     400c61 <main+0x2d>
  666.        a++;
  667.  400c49:       83 45 fc 01             addl   $0x1,-0x4(%rbp)
  668. <span style='color:red'>  400c4d:       48 8b 05 3c 25 20 00    mov    0x20253c(%rip),%rax        # 603190 <dtor_idx.6460+0x8>
  669.  400c54:       48 83 c0 01             add    $0x1,%rax
  670.  400c58:       48 89 05 31 25 20 00    mov    %rax,0x202531(%rip)        # 603190 <dtor_idx.6460+0x8></span>
  671.  400c5f:       eb 16                   jmp    400c77 <main+0x43>
  672.    } else {
  673.        a--;
  674.  400c61:       83 6d fc 01             subl   $0x1,-0x4(%rbp)
  675. <span style='color:red'>  400c65:       48 8b 05 2c 25 20 00    mov    0x20252c(%rip),%rax        # 603198 <dtor_idx.6460+0x10>
  676.  400c6c:       48 83 c0 01             add    $0x1,%rax
  677.  400c70:       48 89 05 21 25 20 00    mov    %rax,0x202521(%rip)        # 603198 <dtor_idx.6460+0x10></span>
  678.    }
  679.  
  680.    return a;
  681.  400c77:       8b 45 fc                mov    -0x4(%rbp),%eax
  682. }
  683.  400c7a:       c9                      leaveq
  684.  400c7b:       c3                      retq
  685. </pre>
  686. <p>From the above disassembly it might seem putting inc instruction while compiling is easy. But how/where storage for the counters(dtor_idx.6460 and dtor_idx.6460 in above example) are created. GCC uses statically allocated memory. Dynamically allocating space is one way but it would complicate the code(memory allocation operations during init) and might slow down execution of program(defer pointer). To avoid that gcc allocates storage as a loadable section.</p>
  687. <p>The compiler keep tracks of all the counters in a single file. The data structure outlined in the below picture.<br />
  688. <a href="http://samueldotj.com/blog/wp-content/uploads/2012/03/gcov-1.png"><img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2012/03/gcov-1.png" alt="gcov" width="577" height="423" class="aligncenter size-full wp-image-53" srcset="http://samueldotj.com/blog/wp-content/uploads/2012/03/gcov-1.png 577w, http://samueldotj.com/blog/wp-content/uploads/2012/03/gcov-1-300x219.png 300w" sizes="(max-width: 577px) 100vw, 577px" /></a><br />
  689. There is a single gcov_info structure for a C file. And multiple gcov_fn_info and gcov_ctr_info. During program exit() these structures are dumped into the .gcda file. For a project(with multiple C files) each C file will have a gcov_info structure. These gcov_info structures should be linked together so that during exit() the program can generate .gcda file for all the C files. This is done by using constructors and destructors.</p>
  690. <p><strong>Generic C constructor</strong>:<br />
  691. gcc generates constructors for all program. C constructors are accomplished by using &#8220;.ctors&#8221; section of ELF file. This section contains array of function pointers. This array is iterated and each function is invoked by _init()->__do_global_ctors_aux() during program start. _init() is placed &#8220;.init&#8221; section so it will be called during program initialization. A function can be declared as constructor by using function <a href="http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Function-Attributes.html">attribute</a>.</p>
  692. <p>&#8220;-ftest-coverage&#8221; creates a constructor per file. This constructor calls __gcov_init() and passes the gcov_info as argument.</p>
  693. <pre class="code">
  694. samuel@ubuntu:~$objdump  -t ./a.out  | grep -i _GLOBAL__
  695. 0000000000400c7c l     F .text  0000000000000010              _GLOBAL__sub_I_65535_0_main
  696. </pre>
  697. <p>And disassembly of _GLOBAL__sub_I_65535_0_main</p>
  698. <pre class="code">
  699. 954 0000000000400c7c <_global__sub_i_65535_0_main>:
  700. 955   400c7c:       55                      push   %rbp
  701. 956   400c7d:       48 89 e5                mov    %rsp,%rbp
  702. 957   400c80:       bf 00 31 60 00          mov    $0x603100,%edi
  703. 958   400c85:       e8 a6 12 00 00          callq  401f30 <__gcov_init>
  704. 959   400c8a:       5d                      pop    %rbp
  705. 960   400c8b:       c3                      retq
  706. 961   400c8c:       90                      nop
  707. 962   400c8d:       90                      nop
  708. 963   400c8e:       90                      nop
  709. 964   400c8f:       90                      nop
  710. </__gcov_init></_global__sub_i_65535_0_main></pre>
  711. <p>gcov_init() implemented in libgcov stores all the gcov_info() passed in a linked list. This linked list is used to walk through all the gcov_info during program termination.</p>
  712. ]]></content>
  713. <link rel="replies" type="text/html" href="http://samueldotj.com/blog/gcov-internals-overview/#comments" thr:count="1" />
  714. <link rel="replies" type="application/atom+xml" href="http://samueldotj.com/blog/gcov-internals-overview/feed/atom/" thr:count="1" />
  715. <thr:total>1</thr:total>
  716. </entry>
  717. <entry>
  718. <author>
  719. <name>samueldotj</name>
  720. </author>
  721.  
  722. <title type="html"><![CDATA[Plot your data using gnuplot]]></title>
  723. <link rel="alternate" type="text/html" href="http://samueldotj.com/blog/graph-your-data/" />
  724.  
  725. <id>http://samueldotj.com/blog/?p=7</id>
  726. <updated>2013-08-26T17:35:10Z</updated>
  727. <published>2012-02-28T04:27:00Z</published>
  728. <category scheme="http://samueldotj.com/blog" term="C" /><category scheme="http://samueldotj.com/blog" term="Programming" /><category scheme="http://samueldotj.com/blog" term="Tools" />
  729. <summary type="html"><![CDATA[System statistics are hard interpolate since usually they are collected in large quantities and sometimes represents large numbers. Recently I was doing a prototype and wanted to measure how much damage it would to the main project (in terms of performance); so used performance counter feature in the processor to measure some events(cache miss, memory [&#8230;]]]></summary>
  730.  
  731. <content type="html" xml:base="http://samueldotj.com/blog/graph-your-data/"><![CDATA[<p>System statistics are hard interpolate since usually they are collected in large quantities and sometimes represents large numbers. Recently I was doing a prototype and wanted to measure how much damage it would to the main project (in terms of performance); so used <a href="http://software.intel.com/en-us/articles/intel-performance-counter-monitor/">performance counter feature</a> in the processor to measure some events(cache miss, memory read etc) with and without my code change. But after looking at the numbers I realized it is difficult to analyze such a data. Because each number is 8 digit and I had 16 columns(16 cpu) and 100 rows of data(100 seconds of run). So I decided to use some graph so that it would be easy to understand the data.</p>
  732. <p>Googled for a GNU graph tool and found <a href="http://gnuplot.info/">gnu plot</a> &#8211; this blog is to show how good it is and how easy it is to use. Consider using it if you have lot of numbers.  For this post I took some sample data from my ubuntu machine while running <a href="http://weather.ou.edu/~apw/projects/stress/">stress </a>command.<br />
  733. [codegroup]<br />
  734. [shell tab=&#8217;output&#8217;]<br />
  735. cat stat.txt<br />
  736. procs &#8212;&#8212;&#8212;&#8211;memory&#8212;&#8212;&#8212;- &#8212;swap&#8211; &#8212;&#8211;io&#8212;- -system&#8211; &#8212;-cpu&#8212;-<br />
  737. r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa<br />
  738. 0  0 251124 1827388   4720  32704    6   34    19    38   42   74  1  0 98  1<br />
  739. 0  0 251124 1827388   4720  32708    0    0     0     0  104   71  0  0 100  0<br />
  740. 13  3 251108 1349912   4728 322540    4    0     4    20  683 1789 42 12 47  0<br />
  741. 11  3 251008 1382620   4728 322520  180    0   180     0 1604 1233 89 12  0  0<br />
  742. 11  3 251008 1432052   4728 322520    0    0     0     0 1361 1237 90 10  0  0<br />
  743. 11  3 251008 1298352   4728 322668    0    0     0     0 1392 1275 90 10  0  0<br />
  744. 2  3 251008 1512576   4728 323524    0    0     0     0 20077 14827 59 16 13 12<br />
  745. 0  0 251008 1826388   4728  32756    0    0     0     0 45069 25566  0  4 25 71<br />
  746. 0  0 251008 1826444   4728  32708    0    0     0     0   59   46  0  0 100  0<br />
  747. &#8230;<br />
  748. [/shell]</p>
  749. <p>[shell tab=&#8217;stress&#8217;]<br />
  750. stress &#8211;cpu 8 &#8211;io 4 &#8211;vm 2 &#8211;vm-bytes 128M &#8211;hdd 2  &#8211;timeout 200s<br />
  751. [/shell]</p>
  752. <p>[shell tab=&#8217;vmstat&#8217;]<br />
  753. sudo vmstat -n 1 200 > stat.txt<br />
  754. [/shell]<br />
  755. [/codegroup]</p>
  756. <p>The following example shows how to create line graph for us, sy columns in the above against time(seconds).<br />
  757. <a href="http://samueldotj.com/blog/wp-content/uploads/2012/02/output.jpg"><img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2012/02/output-300x225.jpg" alt="output" width="300" height="225" class="aligncenter size-medium wp-image-71" srcset="http://samueldotj.com/blog/wp-content/uploads/2012/02/output-300x225.jpg 300w, http://samueldotj.com/blog/wp-content/uploads/2012/02/output.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
  758. <p>This graph might not be impressive because it deals with only numbers ranging from 0-100 and the numbers are very steady. Consider a range 0-99999999 and the numbers are fluctuating too much then it will be to graph.  The above graph was created by running &#8220;gnuplot&#8221; with following commands<br />
  759. [shell]<br />
  760. set title &#8216;CPU usage&#8217;<br />
  761. #set terminal svg butt enhanced dynamic<br />
  762. set terminal jpeg<br />
  763. set output &#8216;output.jpg&#8217;<br />
  764. set xlabel &#8216;seconds&#8217;<br />
  765. #set logscale y<br />
  766. set ylabel &#8216;cpu&#8217;<br />
  767. set key below<br />
  768. plot \<br />
  769.    &#8220;stat.txt&#8221; using :13 title &#8216;Application&#8217; with lines lc rgb &#8216;blue&#8217;, \<br />
  770.    &#8220;stat.txt&#8221; using :14 title &#8216;Kernel&#8217; with lines lc rgb &#8216;green&#8217;<br />
  771. [/shell]</p>
  772. <p>You can also intermix two or more data files. The following example shows how to graph two different samples collected during different time period.<br />
  773. [shell]<br />
  774. set title &#8216;CPU usage&#8217;<br />
  775. #set terminal svg butt enhanced dynamic<br />
  776. set terminal jpeg<br />
  777. set output &#8216;output.jpg&#8217;<br />
  778. set xlabel &#8216;seconds&#8217;<br />
  779. #set logscale y<br />
  780. set ylabel &#8216;cpu&#8217;<br />
  781. set key below<br />
  782. plot \<br />
  783.    &#8220;stat.txt&#8221; using :13 title &#8216;Application&#8217; with lines lc rgb &#8216;light-green&#8217;, \<br />
  784.    &#8220;stat.txt&#8221; using :14 title &#8216;Kernel&#8217; with lines lc rgb &#8216;light-red&#8217;, \<br />
  785.    &#8220;stat1.txt&#8221; using :13 title &#8216;Application1&#8217; with lines lc rgb &#8216;dark-green&#8217;, \<br />
  786.    &#8220;stat1.txt&#8221; using :14 title &#8216;Kernel1&#8217; with lines lc rgb &#8216;dark-red&#8217;<br />
  787. [/shell]<br />
  788. The stat1.txt file is generated by running <a href="http://linux.about.com/library/cmd/blcmdl8_vmstat.htm">vmstat </a>while the system was stressed by the following command<br />
  789. [shell]stress &#8211;cpu 4 &#8211;io 2 &#8211;vm 4 &#8211;vm-bytes 1M &#8211;hdd 2 &#8211;hdd-bytes 4096 &#8211;timeout 200s[/shell]</p>
  790. <p><a href="http://samueldotj.com/blog/wp-content/uploads/2012/02/output-1.jpg"><img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2012/02/output-1-300x225.jpg" alt="Output" width="300" height="225" class="aligncenter size-medium wp-image-70" srcset="http://samueldotj.com/blog/wp-content/uploads/2012/02/output-1-300x225.jpg 300w, http://samueldotj.com/blog/wp-content/uploads/2012/02/output-1.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
  791. <p>The nice thing about gnuplot is it will skip the row(line) in the data file if it cant recognize the columns. And also it supports svg and pdf outputs.  See what all gnuplot can do at the official <a href="http://gnuplot.sourceforge.net/demo/">demo page</a>.</p>
  792. ]]></content>
  793. </entry>
  794. <entry>
  795. <author>
  796. <name>samueldotj</name>
  797. </author>
  798.  
  799. <title type="html"><![CDATA[OpenOCD and NGX USB ARM JTAG]]></title>
  800. <link rel="alternate" type="text/html" href="http://samueldotj.com/blog/openocd-and-ngx-usb-arm-jtag/" />
  801.  
  802. <id>http://samueldotj.com/blog/?p=8</id>
  803. <updated>2013-08-26T17:56:17Z</updated>
  804. <published>2011-05-22T13:04:00Z</published>
  805. <category scheme="http://samueldotj.com/blog" term="C" /><category scheme="http://samueldotj.com/blog" term="Compiler" /><category scheme="http://samueldotj.com/blog" term="Debugger" />
  806. <summary type="html"><![CDATA[This post describes the steps needed to make NGX’s USB ARM JTAG to work with OpenOCD in windows 7. This JTAG is compatible with colink JTAG and works with IAR Workbench and Keil uVision. To use with these IDEs there is a well defined methods/plug-ins available in the product page and in internet. However to [&#8230;]]]></summary>
  807.  
  808. <content type="html" xml:base="http://samueldotj.com/blog/openocd-and-ngx-usb-arm-jtag/"><![CDATA[<div id="attachment_75" style="width: 160px" class="wp-caption alignright"><a href="http://samueldotj.com/blog/wp-content/uploads/2011/05/usb_jtag1.jpg"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-75" src="http://samueldotj.com/blog/wp-content/uploads/2011/05/usb_jtag1-150x150.jpg" alt="NGX’s USB ARM JTAG" width="150" height="150" class="size-thumbnail wp-image-75" /></a><p id="caption-attachment-75" class="wp-caption-text">NGX’s USB ARM JTAG</p></div>
  809. <p>This post describes the steps needed to make <a href="http://shop.ngxtechnologies.com/product_info.php?cPath=26&#038;products_id=30">NGX’s USB ARM JTAG</a> to work with <a href="http://openocd.berlios.de/web/">OpenOCD </a>in windows 7. This <a href="http://en.wikipedia.org/wiki/Joint_Test_Action_Group">JTAG </a>is compatible with <a href="http://www.coocox.org/Colink.htm">colink </a>JTAG and works with<a href="http://www.iar.com/"> IAR Workbench</a> and <a href="http://www.keil.com/uvision/">Keil uVision</a>. To use with these IDEs there is a well defined methods/plug-ins available in the product page and in internet. However to use this JTAG with OpenOCD there is scarce resource in the internet.</p>
  810. <p>OpenOCD can be used to low level debugging, source level debugging (through GDB) and can be used for flashing. OpenOCD exposes a command line interface which can be accessed through telnet. It also provides remote GDB server which also can be reached through TCP connection.</p>
  811. <p>Steps needed for Windows:</p>
  812. <ol>
  813. <li>Plug-In the JTAG to a available USB connector</li>
  814. <li>Download <a href="http://sourceforge.net/projects/libusb-win32/files/">libusb-win32</a></li>
  815. <li>Extract libusb-win32 to a folder and run “inf-wizard.exe”</li>
  816. <li>Select “USB Serial Converter A” and install driver</li>
  817. <li>Download and install <a href="http://www.freddiechopin.info/index.php/en/download/category/4-openocd">OpenOCD</a></li>
  818. <li>Attach the JTAG probe to your target ARM board and poweron the target board</li>
  819. <li>Create a openocd configurations file (see at the end)</li>
  820. <li>Run openocd.exe –f</li>
  821. <li>Run putty or telnet and connect to port localhost:4444</li>
  822. </ol>
  823. <p>After this the target board will respond to JTAG commands which can be issued through the telnet session.</p>
  824. <p>For GDB debugging, you need a cross compiled GDB(<a href="http://www.codesourcery.com/sgpp/lite/arm/portal/subscription?@template=lite">arm-none-eabi-gdb</a>).<br />
  825. After launching arm-none-eabi-gdb.exe run <strong>target remote localhost:3333</strong> to start remote debugging.<br />
  826. You can execute low level JTAG commands from GDB by using <strong>monitor</strong> command.</p>
  827. <p>Flashing can be done using the following commands:<br />
  828. [shell]<br />
  829. reset<br />
  830. halt<br />
  831. sleep 200<br />
  832. wait_halt<br />
  833. flash probe 0<br />
  834. flash info 0<br />
  835. flash write_image erase unlock<br />
  836. sleep 200<br />
  837. reset run<br />
  838. [/shell]</p>
  839. <p>OpenOCD configuration file:<br />
  840. [shell]<br />
  841. # openocd configurations<br />
  842. telnet_port 4444</p>
  843. <p># gdb configuration<br />
  844. gdb_port 3333</p>
  845. <p># cpu configuration<br />
  846. source [find target/lpc1768.cfg]</p>
  847. <p># interface configuration<br />
  848. interface ft2232<br />
  849. ft2232_vid_pid 0x0403 0x6010<br />
  850. ft2232_device_desc &#8220;NGX JTAG&#8221;<br />
  851. ft2232_layout &#8220;oocdlink&#8221;<br />
  852. ft2232_latency 2<br />
  853. [/shell]</p>
  854. ]]></content>
  855. <link rel="replies" type="text/html" href="http://samueldotj.com/blog/openocd-and-ngx-usb-arm-jtag/#comments" thr:count="1" />
  856. <link rel="replies" type="application/atom+xml" href="http://samueldotj.com/blog/openocd-and-ngx-usb-arm-jtag/feed/atom/" thr:count="1" />
  857. <thr:total>1</thr:total>
  858. </entry>
  859. <entry>
  860. <author>
  861. <name>samueldotj</name>
  862. </author>
  863.  
  864. <title type="html"><![CDATA[DIY &#8211; Wirless Router and NAS: Software Pieces]]></title>
  865. <link rel="alternate" type="text/html" href="http://samueldotj.com/blog/wirless-router-network-storage-and-media-server-software-pieces/" />
  866.  
  867. <id>http://samueldotj.com/blog/?p=9</id>
  868. <updated>2013-08-27T01:22:45Z</updated>
  869. <published>2011-03-12T16:43:00Z</published>
  870. <category scheme="http://samueldotj.com/blog" term="Router NAS Linux" /><category scheme="http://samueldotj.com/blog" term="Softwares" />
  871. <summary type="html"><![CDATA[This is the followup post of DIY &#8211; RCN. Here I document about the different software used to make my RCN. Operating System There are two open source choices BSD(FreeBSD) or Linux(ubuntu). After few days of analysis I decided to go with Linux &#8211; because in my work I use FreeBSD. In either case I [&#8230;]]]></summary>
  872.  
  873. <content type="html" xml:base="http://samueldotj.com/blog/wirless-router-network-storage-and-media-server-software-pieces/"><![CDATA[<p>This is the followup post of <a href="http://samueldotj.com/blog/?p=10">DIY &#8211; RCN</a>. Here I document about the different software used to make my RCN.</p>
  874. <h2>Operating System</h2>
  875. <p><a href="http://samueldotj.com/blog/wp-content/uploads/2011/03/linux-ubuntu2.gif"><img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2011/03/linux-ubuntu2.gif" alt="linux-ubuntu" width="48" height="48" class="alignright size-full wp-image-81" /></a> There are two open source choices <a href="http://www.freebsd.org/">BSD</a>(FreeBSD) or <a href="http://www.ubuntu.com/">Linux</a>(ubuntu). After few days of analysis I decided to go with Linux &#8211; because in my work I use FreeBSD. In either case I did not want to use <a href="http://freenas.org/">FreeNAS</a> or <a href="http://www.openfiler.com/">OpenFiler</a> or any other ready made distro. Since I am familiar with Ubuntu, I decided to use the <a href="http://www.ubuntu.com/business/server/overview">Ubuntu server</a> version.</p>
  876. <h3>File System</h3>
  877. <p>Wanted to use <a href="http://en.wikipedia.org/wiki/ZFS">ZFS </a>on my main storage disk but it is not available on Linux yet, so decided to go with <a href="http://en.wikipedia.org/wiki/XFS">XFS</a>.  EXT3/4 on the boot disk because it is natively supported and no extra package needed. The boot media is 8GB flash disk.</p>
  878. <h2>Installation</h2>
  879. <p>Since there is no optical disk drive, installation should be through network or USB. Since most of the Linux distributions supports that I decided to use USB.</p>
  880. <ol>
  881. <li>Download <a href="http://www.ubuntu.com/business/get-ubuntu/download">Ubuntu 10.10 server</a></li>
  882. <li>Download <a href="http://www.pendrivelinux.com/universal-usb-installer-easy-as-1-2-3/">Universal USB installer</a></li>
  883. <li>Create bootable install media using the installer</li>
  884. <li>Boot the system with boot media</li>
  885. </ol>
  886. <h3>Partitions</h3>
  887. <p>Although no data is going to be stored in the boot media, it would be good to have separate partitions to store the config files and home directory. Otherwise re-installation would wipe out all the data.</p>
  888. <p>I chose to create 5 partitions<br />
  889. [shell]<br />
  890. /     &#8211; EXT4 &#8211; 2GB<br />
  891. /usr  &#8211; EXT4 &#8211; 2GB<br />
  892. /var  &#8211; EXT4 &#8211; 2GB<br />
  893. /home &#8211; EXT4 &#8211; 1GB<br />
  894. swap  &#8211;      &#8211; 1GB<br />
  895. [/shell]</p>
  896. <h2>Management</h2>
  897. <p>Since this device will run headless only way to communicate with the system is through network interface. Having SSH access is good but still having a web interface for common administration access is better. Few Linux applications are available for that my choice is <a href="http://www.webmin.com/">Webmin</a>.<br />
  898. [shell]<br />
  899. sudo vi /etc/apt/sources.list<br />
  900. wget http://www.webmin.com/jcameron-key.asc<br />
  901. sudo apt-key add jcameron-key.asc<br />
  902. sudo apt-get update<br />
  903. sudo apt-get install webmin<br />
  904. [/shell]<br />
  905. After this the machine can be controlled from local network &#8211; https://hostname:10000/</p>
  906. <h3>Shutdown</h3>
  907. <p>Shutting down the system should be easy. Since the storage is connected to the system it cant be power off directly. The file system data should be syncd first and using command line or web interface is not realistic. So programming the ATX power switch is the only way &#8211; acpid does that.<br />
  908. [shell]<br />
  909. sudo apt-get install acpid<br />
  910. [/shell]</p>
  911. <h2>Storage</h2>
  912. <p>The goal was to create file based storage which is accessible from my home network. The <a href="http://en.wikipedia.org/wiki/Network-attached_storage">NAS server</a> should be big enough for at least next 2 years(1TB). It should be fast enough to view videos from it without flickering(64MB ondisk buffer). It should have hardware fault tolerance(<a href="http://en.wikipedia.org/wiki/RAID">RAID</a>).</p>
  913. <p>Although few of my desktop boards had RAID option in the BIOS menu, I never used it and never explored it. I thought RAID chipsets in a motherboard is equivalent to RAID controllers/adapters. It was one of the decideding factor I favoured for Gigabyte(GA-D425TUD) motherboard with JMicron RAID chipset over Intel(D525MO) motherboard.</p>
  914. <p>After configuring RAID in the BIOS and starting Linux I realized it is not true raid. Because Linux recognized as fakeraid. In simple terms fakeraid is a firmware based RAID. That is all the work is still to be done in software yielding no performance benefit. Advantage of fakeraid is multiple OS which runs on same box can utiltize the same RAID. Since my setup wont have multiboot option, I dont want the fakeraid so decided to go with pure software RAID 0. Here is the steps to create software raid 0.</p>
  915. <ol>
  916. <li>Create software raid using multiple devices(md) interface.</li>
  917. <p>       [shell]mdadm &#8211;create &#8211;verbose /dev/md0 &#8211;level=1 &#8211;raid-devices=2 /dev/sda /dev/sdb[/shell]</p>
  918. <li>The above command will take some time (around 6 hours) because it needs to sync the contents of both disks.<br />
  919.       While it is doing that the status can be checked by using the following command.</li>
  920. <p>       [shell]cat /proc/mdstat[/shell]</p>
  921. <li>Then create a XFS file system on the md device</li>
  922. <p>       [shell]mkfs.xfs /dev/md0[/shell]</p>
  923. <li>Store the configuration</li>
  924. <p>       [shell]<b>mdadm &#8211;detail &#8211;scan > /etc/mdadm/mdadm.conf[/shell]</p>
  925. <li>Create mount point and add the mount information in the /etc/fstab</li>
  926. <p>   [shell]<br />
  927.     mkdir /mnt/raid<br />
  928.     echo &#8220;/dev/md0        /mnt/raid       xfs     defaults            1       2&#8221; >> /etc/fstab<br />
  929.   [/shell]
  930. </ol>
  931. <h3>Windows File Sharing</h3>
  932. <p>After this /mnt/raid can be made accessible to remote machines through either NFS or through Windows File Sharing. For Windows File Sharing <a href="http://www.samba.org/">samba </a>service needed to installed. The following command installs samba server.<br />
  933. [shell]sudo apt-get install samba[/shell]</p>
  934. <p>After installing samba server it can be configured using webmin. Use webmin to configure samba “Servers”->”Samba File sharing”. Add the storage mount point here.</p>
  935. <h2>Router</h2>
  936. <p>The routing functionality is very simple &#8211; handle all 3 interfaces with some limitations.<br />
  937. <a href="http://samueldotj.com/blog/wp-content/uploads/2011/03/RCN.png"><img loading="lazy" decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2011/03/RCN-300x148.png" alt="RCN" width="300" height="148" class="alignright size-medium wp-image-85" srcset="http://samueldotj.com/blog/wp-content/uploads/2011/03/RCN-300x148.png 300w, http://samueldotj.com/blog/wp-content/uploads/2011/03/RCN.png 427w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
  938. <ul>
  939. <li>First interface eth0 is a Gigabit ethernet interface which is directly connected to the a desktop computer.</li>
  940. <li>Second interface eth1 is a Fast ethernet interface which is directly connected to internet(connected to a ADSL modem).</li>
  941. <li>Third interface is 802.11n wireless network.</li>
  942. </ul>
  943. <h3>Network and IP</h3>
  944. <p>All interfaces are in different networks. All interface should get static IPv4 address while booting up. This router should provide dynamic IP to the other machines.</p>
  945. <p>Modify network interface and dhcp configurations<br />
  946. [codegroup]<br />
  947. [shell tab=&#8221;/etc/network/interface&#8221;]<br />
  948. # This file describes the network interfaces available on your system<br />
  949. # and how to activate them. For more information, see interfaces(5).</p>
  950. <p># The loopback network interface<br />
  951. auto lo<br />
  952. iface lo inet loopback</p>
  953. <p># The primary network interface<br />
  954. auto eth0<br />
  955. iface eth0 inet static<br />
  956. address 192.168.1.2<br />
  957. netmask 255.255.255.0<br />
  958. gateway 192.168.100.2<br />
  959. post-up iptables-restore < /etc/iptables.up.rules
  960. up /etc/init.d/dhcp3-server start
  961.  
  962. #wireless network
  963. auto wlan0
  964. iface wlan0 inet static
  965. address 192.168.2.1
  966. netmask 255.255.255.0
  967. gateway 192.168.100.2
  968. up /etc/init.d/dhcp3-server start
  969.  
  970. #wan interface
  971. auto eth1
  972. iface eth1 inet static
  973. address 192.168.100.2
  974. netmask 255.255.255.0
  975. gateway 192.168.100.1
  976. [/shell]
  977.  
  978. [shell tab='/etc/dhcp3/dhcpd.conf']
  979. subnet 192.168.1.0 netmask 255.255.255.0 {
  980. range 192.168.1.100 192.168.1.200;
  981. option domain-name-servers 208.67.222.222, 208.67.220.220;
  982. option routers 192.168.1.2;
  983. option broadcast-address 192.168.1.255;
  984. default-lease-time 600;
  985. max-lease-time 7200;
  986. }
  987. subnet 192.168.2.0 netmask 255.255.255.0 {
  988. range 192.168.2.100 192.168.2.200;
  989. option domain-name-servers 208.67.222.222, 208.67.220.220;
  990. option routers 192.168.2.1;
  991. option broadcast-address 192.168.2.255;
  992. default-lease-time 600;
  993. max-lease-time 7200;
  994. }
  995. [/shell]
  996. [/codegroup]
  997.  
  998. Finally enable forwarding in Linux kernel by setting a system tunable.
  999. [shell]echo 1 > /proc/sys/net/ipv4/ip_forward[/shell]</p>
  1000. <p>To set it during boot modify <b>/etc/sysctl.conf</b></p>
  1001. <h3><a href="http://en.wikipedia.org/wiki/Network_address_translation">NAT &#8211; Network Address Translation</a></h3>
  1002. <p>NAT is required on eth1 to translate addresses on any outgoing packets and incoming packets. For the iptable rules should be set correctly, the following script does that.<br />
  1003. [shell]<br />
  1004. INTIF=&#8221;eth0&#8243;<br />
  1005. EXTIF=&#8221;eth1&#8243;</p>
  1006. <p>#set default polices and flush<br />
  1007. iptables -P INPUT ACCEPT<br />
  1008. iptables -F INPUT<br />
  1009. iptables -P OUTPUT ACCEPT<br />
  1010. iptables -F OUTPUT<br />
  1011. iptables -P FORWARD DROP<br />
  1012. iptables -F FORWARD<br />
  1013. #setup NAT<br />
  1014. iptables -t nat -F<br />
  1015. iptables -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE</p>
  1016. <p>iptables -A FORWARD -i $EXTIF -o $INTIF -m state &#8211;state ESTABLISHED,RELATED -j ACCEPT<br />
  1017. iptables -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT</p>
  1018. <p>INTIF1=&#8221;wlan0&#8243;<br />
  1019. iptables -A FORWARD -i $EXTIF -o $INTIF1 -m state &#8211;state ESTABLISHED,RELATED -j ACCEPT<br />
  1020. iptables -A FORWARD -i $INTIF1 -o $EXTIF -j ACCEPT</p>
  1021. <p>iptables -A FORWARD -i $INTIF -o $INTIF1 -j ACCEPT<br />
  1022. iptables -A FORWARD -i $INTIF1 -o $INTIF -j ACCEPT</p>
  1023. <p>#unblock certain services<br />
  1024. #webmin<br />
  1025. iptables -A INPUT -p tcp -m tcp &#8211;dport 10000 -j ACCEPT<br />
  1026. [/shell]</p>
  1027. <h3>Wireless</h3>
  1028. <p><a href="http://samueldotj.com/blog/wp-content/uploads/2011/03/wifi.jpg"><img decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2011/03/wifi.jpg" alt="wifi" width="125" class="alignright size-medium wp-image-82" /></a>Now it is time to setup the wireless interface. Assuming the wireless are drivers are present in the kernel.<br />
  1029. The other tool required is <b>hostapd</b>. hostapd implements IEEE 802.11 access point management.<br />
  1030. hostapd configuration<br />
  1031. [shell]<br />
  1032. interface=wlan0<br />
  1033. driver=nl80211</p>
  1034. <p>ctrl_interface=/var/run/hostapd<br />
  1035. ctrl_interface_group=0</p>
  1036. <p>ssid=rcnap<br />
  1037. hw_mode=g<br />
  1038. channel=11</p>
  1039. <p>ieee80211n=1<br />
  1040. #ht_capab=[HT40-][SHORT-GI-40]</p>
  1041. <p>wpa_pairwise=TKIP CCMP<br />
  1042. wpa=1<br />
  1043. [/shell]</p>
  1044. ]]></content>
  1045. </entry>
  1046. <entry>
  1047. <author>
  1048. <name>samueldotj</name>
  1049. </author>
  1050.  
  1051. <title type="html"><![CDATA[DIY – Wirless Router and NAS: Hardware bits]]></title>
  1052. <link rel="alternate" type="text/html" href="http://samueldotj.com/blog/how-to-create-your-own-wireless-router-network-storage-server-and-media-server/" />
  1053.  
  1054. <id>http://samueldotj.com/blog/?p=10</id>
  1055. <updated>2013-08-26T19:19:36Z</updated>
  1056. <published>2011-02-14T16:20:00Z</published>
  1057. <category scheme="http://samueldotj.com/blog" term="Router NAS Linux" />
  1058. <summary type="html"><![CDATA[I have replaced my Buffalo WRT-G54 wireless router with my own custom built router. This blog explains the details involved that process. I call this device as RCN (Router cum NAS). Hardware I began my hardware hunt 45 days back – it was difficult because of my requirements and availability in Indian Market. Here is [&#8230;]]]></summary>
  1059.  
  1060. <content type="html" xml:base="http://samueldotj.com/blog/how-to-create-your-own-wireless-router-network-storage-server-and-media-server/"><![CDATA[<p>I have replaced my Buffalo WRT-G54 wireless router with my own custom built router. This blog explains the details involved that process. I call this device as RCN (Router cum NAS).</p>
  1061. <h2>Hardware</h2>
  1062. <p>I began my hardware hunt 45 days back – it was difficult because of my requirements and availability in Indian Market.</p>
  1063. <p>Here is the initial requirement list I had(strike-through indicates I gave up that requirement because the items are too costly or not available in India):</p>
  1064. <ul>
  1065. <li>Total board TDP should be less than 40 watts</li>
  1066. <li><s>Passively cooled</s></li>
  1067. <li>Mini-ITX form factor</li>
  1068. <li>Onboard RAID</li>
  1069. <li>DDR3 support</li>
  1070. <li><s>Dual core</s></li>
  1071. <li>Hardware should be available in India</li>
  1072. </ul>
  1073. <p>Here is the final products I purchased:</p>
  1074. <ul>
  1075. <li><a href="http://www.gigabyte.com/products/product-page.aspx?pid=3550#ov">Gigabyte GA-D425TUD</a></li>
  1076. <li><a href="http://iball.co.in/Product.aspx?c=5">iBall Baby 306</a> Cabinet</li>
  1077. <li>DDR-3 2GB RAM</li>
  1078. <li>2 x 1TB Western Digital <a href="http://www.wdc.com/en/products/products.aspx?id=120">WD10EARS</a> SATA</li>
  1079. <li>USB to Ethernet</li>
  1080. <li>USB Flash Drive</li>
  1081. <li>PCI Wifi Adapter <a href="http://www.dlink.com/products/?pid=531">DLink 552 Xtreme N</a></li>
  1082. </ul>
  1083. <p>Here is some pictures of my RCN</p>
  1084. <p><a href="http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-012.jpg"><img decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-012-300x200.jpg" alt="RCN 012" width="150"  class="alignright size-medium wp-image-92" srcset="http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-012-300x200.jpg 300w, http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-012-1024x682.jpg 1024w, http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-012.jpg 1600w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
  1085. <p><a href="http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-0061.jpg"><img decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-0061-300x200.jpg" alt="RCN 006" width="150" class="alignright size-medium wp-image-94" srcset="http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-0061-300x200.jpg 300w, http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-0061-1024x682.jpg 1024w, http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-0061.jpg 1600w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
  1086. <p><a href="http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-013.jpg"><img decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-013-300x200.jpg" alt="RCN 013" width="150" class="alignright size-medium wp-image-93" srcset="http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-013-300x200.jpg 300w, http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-013-1024x682.jpg 1024w, http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-013.jpg 1600w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
  1087. <p><a href="http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-010.jpg"><img decoding="async" src="http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-010-300x200.jpg" alt="RCN 010" width="150" class="alignright size-medium wp-image-91" srcset="http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-010-300x200.jpg 300w, http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-010-1024x682.jpg 1024w, http://samueldotj.com/blog/wp-content/uploads/2011/02/RCN-010.jpg 1600w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
  1088. <p>Interesting fact about <a href="http://www.wdc.com/en/products/products.aspx?id=120">WD10EARS</a> is it use 4KB sectors instead of the conventional 512byte sectors. Since this is a breakthrough software support is not much there. Looks like these drives perform poor if not partitioned properly &#8211; Since my requirement demands creating single partition, I purchased this drive and it performs well.</p>
  1089. ]]></content>
  1090. <link rel="replies" type="text/html" href="http://samueldotj.com/blog/how-to-create-your-own-wireless-router-network-storage-server-and-media-server/#comments" thr:count="1" />
  1091. <link rel="replies" type="application/atom+xml" href="http://samueldotj.com/blog/how-to-create-your-own-wireless-router-network-storage-server-and-media-server/feed/atom/" thr:count="1" />
  1092. <thr:total>1</thr:total>
  1093. </entry>
  1094. </feed>
  1095.  

If you would like to create a banner that links to this page (i.e. this validation result), do the following:

  1. Download the "valid Atom 1.0" banner.

  2. Upload the image to your own server. (This step is important. Please do not link directly to the image on this server.)

  3. Add this HTML to your page (change the image src attribute if necessary):

If you would like to create a text link instead, here is the URL you can use:

http://www.feedvalidator.org/check.cgi?url=http%3A//samueldotj.com/blog/%3Ffeed%3Datom

Copyright © 2002-9 Sam Ruby, Mark Pilgrim, Joseph Walton, and Phil Ringnalda