Categories
Blog Low Latency Technology

Peer 2 Peer Trading Demo

            SMR is building a peer 2 peer trading platform for trading on foreign exchange. This video shows two traders buy and selling from each other. For technically minded the app is built as an AngularJS single page app with web socket REST API Scala / Java server with Cassandra as the data store.

 

You can also see in on YouTube

 

Categories
Java Technology

Java Tuning and Trouble Shooting

            <h2>Useful JVM Settings</h2>

-Xmx512m (maximum Heap space) -Xms1024m (minimum Heap size)
starting points 2G 32 bit 3-4G 64 bit and 1:3 young vs old
-XX:+HeapDumpOnOutOfMemoryError

Java Trouble Shooting Tools

  • heap dump
    • jmap -dump:file=filename.bin [pid]
      • hprof binary format
      • needs to be analysed in another took like MAT
      • or jhat to see in a web browser on port 7000 on local host
        • jhat -J-mx2000m filename.bin
    • jmap -histo [pid]
      • histogram of java object heap
      • quick text output
    • jmap -heap [pid]
      • to print java heap summary
  • thread dump / thread stack
    • jstack [pid]
  • enabling gc logs
    • -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:[logname]
  • visualJVM
  • jconsole
  • list open files for process
    • sudo lsof -p [pid]
  • list max open file limit
    • ulimit -S -n
Categories
Technology

Working Example C++ Addin for Excel on Mac OS X using CDT

            <h2>Introduction</h2>

There are some tutorials that try to explain how to create an Excel Addin for Mac OS X but none of the give you a working example for the Mac, and most addin tutorials are aimed at Windows. So I have created a working example which you can find here. This short article gives a bit more explanation on what was needed to make the example work.

Note. These instructions only work for Mac OS X.

Prerequisites and Installation

Install CDT (I didn’t try installing the Eclipse CDT plugin but apparently it works too). I used CDT Mars Release (4.5.0).

  • Download site: https://eclipse.org/cdt/downloads.php
    • standalone: http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/marsr
    • update site: http://download.eclipse.org/tools/cdt/releases/8.7

Install Xcode, if not already installed. I used Version 6.4 (6E35b) with all updates applied.

Buy and install Excel Mac 2011, if not already installed.

Start Eclipse CDT and follow the instructions in README.md to import three projects into Eclipse. You should see this when you have finished:

cdt-projects

The C++ Addin – MacExampleAddin

The addin justs adds two doubles together. Its declared in addin.hpp and defined in addin.cpp. In fact we are not using C++ directly but we still use C++ file naming conventions which makes the compiler think we are using C++.

The project builds a 32 bit dylib and was created by choosing the Shared Library -> Empty Project  -> MacOSX GCC in the new Eclipse C++ project dialogue box. Important: You must build a 32 bit shared library for Excel 2011 which is a 32 bit app. So some changes were made to the settings (Eclipse menu -> Project -> Properties) as follows :

  • In C/C++ Build -> Settings Tab make the following changes:
    • MacOS X C++ Linker -> clang++
      • Dialect -> ISO C++11 (-std=c++0x)
      • Miscellaneous  -> -arch i386 -fvisibility=hidden
    • GCC C++ Complier -> clang++
      • Dialect -> ISO C++11 (-std=c++0x)
      • Miscellaneous  -> -c -fmessage-length=0 -fvisibility=hidden -arch i386
    • GCC C Complier -> clang
      • Dialect -> ISO C11 (-std=c11)
      • Miscellaneous  -> -c -fmessage-length=0 -fvisibility=hidden -arch i386

One of my objectives was to use C++11 but I couldn’t be bothered downloading the gcc compilers so I tried clang which is part of Xcode. After a bit of research I discovered the sensible people that wrote clang made the options the same as gcc so that either compiler can be used in place of the other. Eclipse CDT is more gcc focused so doesn’t know about clang which is why the settings changes below were made. As you’ll see, if you try this approach, clang works very well.

If the settings don’t work for you either I’ve missed something in the write up here so check the settings in Eclipse or time has marched on and versions have changed and this tutorial no longer works 🙁

addin.hpp

I declared the functions in an extern “C” directive to ensure the compiler doesn’t mangle the names as it will due to the hpp extension. This will make life simpler when we actually use the addin in Excel as the names in the header will be the same in the shared library. There are actually two functions on the header: addNum and addNum2 which will be explained in a moment but they do the same thing, add two numbers together.

addin.cpp

The file defines EXPORT which is a compiler/linker directive that allows us to selectively export functions in the library along with the appropriate compiler / linker options, that is “-fvisibility=hidden”.

addNum is also wrapped in an extern directive and preceded by the EXPORT to ensure its visible outside of the shared library.

addNum2 is similarly wrapped by extern and EXPORT but calls a function that is invisible in the shared library called notExported. I only did this to show it is possible.

If you examine the library with nm -gU you will see that notExported is in fact not exported which means we aren’t polluting the namespace for our clients.

nm -gU libMacExampleAddin.dylib
00000f40 T _addNum
00000f70 T _addNum2

The nm options are:
-g     Display only global (external) symbols.
-U     Don’t display undefined symbols.

The Excel Spreadsheet using the Addin – MacExampleExcel

Before you start Excel copy MacExampleAddin/Debug/libMacExampleAddin.dylib to the MacExampleExcel directory. When you start Excel you’ll need to accept macros, but don’t worry there’s nothing bad in there, just some VBA to access the shared library.

addin-test.xlsm

When you start Excel it will ask you to accept macros (say yes) but it may not start the VBA editor so select menu Tools -> Macro -> Visual Basic Editor (I’ve exported the basic module here).

As you can see there are two sorts of functions, Private and non-private functions. The private ones are not visible to Excel but the rest are. The reason for separation is because that’s the only way I could get it to work, but conveniently the shared library functions are wrapped and hidden in the private functions and referenced by Lib “….”. The public functions simply call the private ones to return the values you see in the spreadsheet.

As you’ll see I’ve shown functions with absolute paths (addFunctionAbsolute) which only work for me. Yes, not good but it shows it works and you can change it if you want to make it work for your Mac. Secondly there are examples that assume a library is present in the same directory as the Excel spread sheet (addFunctionLocal). These also work if you look at the spreadsheet. Finally there are relative paths (addFunctionRelative) which don’t work but should, but don’t know why.

But what you should see is this:

cdt-excel

 

The C Main program that uses the Addin – MacExampleMain

This is a simple  main program again generated by CDT but as a “Hello World C++ Project” with MacOSX GCC tool chains. I shan’t detail all the changes as you can look at them yourself but basically they are:

  • added -arch i386 to all the tools
  • added the include path to MacExampleAddin/src
  • added the library path to MacExampleAddin/Debug
  • added the library MacExampleAddin (without the preceding lib and .dylib extension)
  • used clang++ and C++11
  • used clang and C11
  • changed main.cpp to test addNum as follows:
if (addNum(1,2) != 3) {
  cerr << "FAIL: Wrong number from addnum expected 3 and got " << addNum(1,2) << endl;
}

For your convenience there is an Eclipse launch that sets up DYLD_LIBRARY_PATH path to allow you to run the project in Eclipse as follows:

Run -> Run History -> MacExampleMain

You should see this in the console:

PASS: Correct number from addnum expected 3 and got 3

You need to set DYLD_LIBRARY_PATH to point to the directory where the dylib is if you intend to run the program in a terminal window:

peter$ ./Debug/MacExampleMain 
dyld: Library not loaded: libMacExampleAddin.dylib
  Referenced from: /Users/peter/Documents/eclipse/workspace-cdt/MacCPPExcelAddinExample/main/./Debug/MacExampleMain
  Reason: image not found
Trace/BPT trap: 5

Fails because the library can’t be found.

peter$ export DYLD_LIBRARY_PATH=../addin/Debug/
peter$ ./Debug/MacExampleMain 
PASS: Correct number from addnum expected 3 and got 3

This works because the library can be found.

But you don’t need to set DYLD_LIBRARY_PATH  if the library is in the current working directory:

peter$ ls -l
-rwxr-xr-x  1 peter  staff  17312 30 Jul 21:04 MacExampleMain
-rwxr-xr-x  1 peter  staff   4704 30 Jul 21:04 libMacExampleAddin.dylib

peter$ ./MacExampleMain 
PASS: Correct number from addnum expected 3 and got 3

 

© Copyright 2015, Peter Lappo, SMR Ltd.

Categories
Technology

Git Branching

            I like to use are two kinds of branches, features (aka enhancement / story) and issues. A single feature can encapsulate a group of enhancements or even a sprint. We can be flexible here as creating too many branches will slow us down. An issue is a bug, a configuration change or some kind of task that involves a code change.

The naming convention is:-
feature-name e.g. feature-add-currency-prices
issue-name e.g. issue-prices-not-appearing

Seeing which Branches are Available

git branch
Or for remote branches not in your repository
git branch -a

Creating a Branch

To create a branch on an project that you have previously cloned and as a side effect also switch to the branch
git checkout -b feature-add-currency-prices
Make some changes, commit them in the usual way. Then push to the repository and add the branch to the repository so that others can see it
git push -u origin feature-add-currency-prices
And simply do
git push
subsequently

Using an Existing Branch

To switch to an existing branch first make sure you have the latest repo which includes the branch
git pull
Then checkout the branch and switch to it
git checkout feature-add-currency-prices
Make git changes and commit in the usual way and then when its time to push
git push -u origin feature-add-currency-prices
After that git remembers your branch so you can simply type
git push

Merging to Master

Finally merge to master
git checkout master
git pull
git pull origin feature-add-currency-prices
git push origin master

Merging changes from Master to a branch

git checkout master
git pull
git checkout feature-add-currency-prices
git pull origin feature-add-currency-prices
git merge master
-- there maybe conflicts at this point
git status
-- after all conflicts are resolved
git push origin feature-add-currency-prices

Issues

Eclipse didn’t do a good job of branching but once the branch is setup it does recognise the branch and can be used for commit and after the first manual push.

Tags

To tag the master after a successful test to say v-0.2.6

git tag -a v-0.2.6 -m 'v-0.2.6'
git push origin v-0.2.6

Tips

To see what commits are waiting to be pushed
git log --branches --not --remotes
For a list of files to be pushed, run
git diff --stat --cached origin/master
To delete a branch
git branch -d branch_name
To remove from origin
git push origin :branch_name

References

https://www.atlassian.com/git/workflows#!workflow-feature-branch
http://reinh.com/blog/2009/03/02/a-git-workflow-for-agile-teams.html
http://marklodato.github.io/visual-git-guide/index-en.html
http://nvie.com/posts/a-successful-git-branching-model
http://rogerdudler.github.io/git-guide/

Categories
NoSQL Technology

Cassandra Data Modelling Videos

            Watch this first...<a href="http://www.slideshare.net/patrickmcfadin/the-data-model-is-dead-long-live-the-data-model">The data model is dead long live the data model</a> and <a href="https://www.youtube.com/watch?v=px6U2n74q3g"> video</a>

And then….The worlds next top data model and video

Finally…Become a super modeler and video

Other modelling videos including the three above

Categories
Gradle Technology

Maven Central Gradle Guide

            Sign up to <a href="https://docs.sonatype.org/display/Repository/Sonatype+OSS+Maven+Repository+Usage+Guide">sonatype</a> first and checkout this <a href="http://jedicoder.blogspot.co.uk/2011/11/automated-gradle-project-deployment-to.html">guide</a>. Copy suitable bits from this working <a href="https://github.com/GradleFx/GradleFx/blob/develop/build.gradle">gradle build </a>file and see <a href="https://github.com/smr-co-uk/java-logonce">LogOnce</a>.

Will also need a PGP key, install GPG and GPG user guide and sonatype PGP help is here.

 

Categories
Java Technology

Big O Times

            <table width="688" border="0" cellspacing="0" cellpadding="0"><colgroup> <col width="75" /> <col width="71" /> <col width="72" /> <col width="73" /> <col width="76" /> <col width="106" /> <col width="109" /> <col width="106" /> </colgroup>
n O(1) O(log(n)) O(n) O(n log(n)) O(N^2) O(2^N) O(N!) 1 1 0.0 1 0.0 1 2 1 5 1 0.7 5 3.5 25 32 120 10 1 1.0 10 10.0 100 1024 3628800 25 1 1.4 25 34.9 625 33554432 1.55112E+25 50 1 1.7 50 84.9 2,500 1.1259E+15 3.04141E+64 100 1 2.0 100 200.0 10,000 1.26765E+30 9.3326E+157 500 1 2.7 500 1,349.5 250,000 3.2734E+150 too large for excel 1000 1 3.0 1,000 3,000.0 1,000,000 1.0715E+301 too large for excel 10000 1 4.0 10,000 40,000.0 100,000,000 too large for excel too large for excel 100000 1 5.0 100,000 500,000.0 10,000,000,000 too large for excel too large for excel
Categories
Java Technology

Java Data Structures

            Here are list Java 7 data structures and their thread model and expected performance. (<span style="line-height: 1.714285714; font-size: 1rem;">See </span><a style="line-height: 1.714285714; font-size: 1rem;" href="http://bigocheatsheet.com/">BigO Cheat Sheet</a><span style="line-height: 1.714285714; font-size: 1rem;"> for more algorithm speeds):-</span>
Name Overiew Threading Method Sorted Index Search / Contiains or Get Insert Delete Who
Maps
HashMap Hashtable Not thread safe No n/a O(1) O(1) O(1) Java
ConcurrentHashMap Segmented hashtable Non blocking No n/a Java
Hashtable Old School, don’t use Intrinsic blocks n/a O(1) O(1) O(1) Java
ConcurrentSkipListMap Tree like skipped list Lock free CAS Yes n/a O(log(N)) O(log(N)) O(log(N)) Java
Lists
ArrayList Resizable array Not thread safe No O(1) O(N) O(N) O(N) Java
LinkedList Double linked list Not thread safe No O(N) O(N) O(1) O(1) Java
Vector Old School, don’t use Intrinsic blocks No O(N) O(N) O(1) O(1) Java
Sets
HashSet HashMap backed Not thread safe No n/a O(1) O(1) O(1) Java
TresSet Tree backed Not thread safe Yes n/a O(log(N)) O(log(N)) O(log(N)) Java
ConcurrentSkipListSet As ConcurrentSkipListMap As ConcurrentSkipListMap yes n/a O(log(N)) O(log(N)) O(log(N)) Java
Queue  / FIFO
ConcurrentLinkedQueue Non blocking No Java
PriorityQueue Balanced binary heap Not thread safe Yes n/a O(N) O(log(N)) O(log(N)) Java
ArrayBlockingQueue Bounded array Reentrant lock No O(1) O(N) O(1) O(N) Java
PriorityBlockingQueue Array based binary heap Reentrant lock Yes n/a O(N) O(log(N)) ? O(log(N)) ? Java
LinkedBlockingQueue Typically greater throughput than array backed Reentrant lock No Java
LinkedTransferQueue Producer waits for consumer Short spin then park/unpark No Java
SynchronousQueue Insert waits for remove Short spin then park/unpark No Java
DelayQueue Backed by PriorityQueue Reentrant lock Yes Java
ForwardingBlockingQueue Blocking queue to another Blocking queue Not thread safe No Google
EvictingQueue Evicts from head Not thread safe No Google
Dequeue / FIFO or LIFO
LinkedList List, double linked list Not thread safe No O(N) O(N) O(1) O(1) Java
LinkedBlockingDeque Linked list Reentrant lock No Java
ArrayDeque Array backed Not thread safe No Java
ConcurrentLinkedDeque Non blocking No Java
ForwardingDeque Abstract Not thread safe No Google
ForwardingBlockingDeque Abstract Not thread safe No Google
Categories
Java Low Latency Technology

Cost of Locks, CAS and Memory Access

            The absolute numbers below will change as technology progresses but the relative numbers should stay the same.

According to LMAX the relative cost of locks and CAS is as follows (May 2011):-

Operation (500 million times) Cost (ms) Relative Cost
Single thread 300 1
Single thread with lock 10000 33
Two threads with lock 224000 747
Single thread with CAS 5700 19
Two threads with CAS 30000 100
Single thread with volatile write 4700 16
And according to Pinku Surana (Handwaving), here are the numbers for memory access (Jan 2009):-
Operation Cost (ns) Relative Cost
L1 cache reference 0.5 1
Branch mispredict 5 10
L2 cache reference 7 14
Mutex lock/unlock 100 200
Main memory reference 100 200
Compress 1K bytes with Zippy 10,000 20,000
Send 2K bytes over 1 Gbps network 20,000 40,000
Read 1 MB sequentially from memory 250,000 500,000
Round trip within same datacenter 500,000 1,000,000
Disk seek 10,000,000 20,000,000
Read 1 MB sequentially from network 10,000,000 20,000,000
Read 1 MB sequentially from disk 30,000,000 60,000,000
Send packet CA->Netherlands->CA 150,000,000 300,000,000
Categories
Low Latency Technology

Performance Testing Low Latency Systems

            <div title="Page 1">

Statistical Tests to Assess Randomized Algorithms A useful article saying that 30 runs (the behavioural science number) is not enough and 1000 runs should be used. Based on central limit theorem. For a useful video and a tool to play with the sizes for CL theorem.