Monday, February 29, 2016

How to visualize UI hanging using Concurrency Visualizer in VS 2013

Fx 4.5 Aysnc-Wait allow UI Processing 80%+ of time, while Fx 4.0 Blocking Collection only allow 20%-



Ring Buffer looks better in UI Processing more like 40% (a comparison of different logging code writing log intensively on a WPF Form, seems like comparing in memory buffer vs. async-await thread suspension)

How much dotTrace .dtp snapshot can help UI slow and freeze issues

(1)time distribution, rollup and hotspot cannot identify which functions causes UI slow directly. In fact, hotspot list will overrun/hide problem.  e.g. User experience slow/wrong updates on Datagrid. That would not be hot but slow, and there are no way that will come up on your list.
      Only WPF Performance suite can identify slowness by frame rate comparison when sending fake data to two UI portions.

(2) But if you know your codebase well, then trace down using time distribution would be great. e.g Startup is slow and you can find the code path to walk down critical path using own time legend.

(3) Threads/Calls compare children time vs. parent rollup up time. e.g. function "DispatchQuote" 15% sub-divide to "BlockCollection" 15%, "OnQuoteChanged" 0%. This particularly indicated that Data did not overwhelm UI since it seems to have slowed down by blocking. So guessing through code on ViewModel Properties.
 
(4) There is a legend to identify threads doing message pumping. Some of these threads run user code waiting for background data. So could be candidate for BeginInvoke so UI would be more responsive and also not allow two UI element trying get to the top. In particular, Main Thread does message pumping and could have a lot of user code. That could help trace where UI Blocking happens.

(5) Adjust time is a work planning tools, no useful for diagnostics.


NSubstibute and Moq Comparison


(1) Mock<T>.Object is need to get T, while Substitute<T>.For is T
 (2) Mock function calls "Faking" go through SetUp(Func).Returns, while NSubstiture only need .Returns
 (3) The actual calls are the same
 (4) Mock has VerifyAll(), NSubstibute does not.

    [TestClass]
     public class UnitTest1
     {
         IListing listing = Substitute.For<IListing>();
         IMarketDepthDataService depthDataService = Substitute.For<IMarketDepthDataService>();

        Mock<IListing> listing2 = new Mock<IListing>();
         private Mock<IMarketDepthDataService> depthDataService2 = new Mock<IMarketDepthDataService>();

        [TestInitialize ]
         public void Init()
         {
             depthDataService.GetDepthForListing(listing, 5).Returns(i => new ListingMarketDepthData(listing, 5));
             depthDataService2.Setup(i => i.GetDepthForListing(listing2.Object, 5)).Returns(new ListingMarketDepthData(listing2.Object, 5));
         }

        [TestCleanup]
         public void Cleanup()  // have to be Public to get called
         {
            
         }

        [TestMethod]
         public void TestMethod1()
         {
             var data = depthDataService.GetDepthForListing(listing, 5);

            var data2 = depthDataService2.Object.GetDepthForListing(listing2.Object, 5);

            Assert.IsTrue(data2.Summary == "3_Year,Depth=10" && data.Summary == "3_Year,Depth=10");

            depthDataService2.VerifyAll();
         }
     }

    public interface IListing
     {
         string Name { get; set; }
     }

    public interface IMarketDepthDataService
     {
         ListingMarketDepthData GetDepthForListing(IListing listing, int level);
     }

    public class ListingMarketDepthData
     {
         public ListingMarketDepthData(IListing listing, int maxDepth)
         {
             Summary = "3_Year,Depth=10";
         }

        public string Summary { get; set; }
     }


Saturday, February 27, 2016

bash scripting braindump

#!/bin/bash #!/bin/bash -x chmod u-x tryout.sh type test chmod a+x echo "$1$(date)>> ~/notes.txt $USER $user var3="$var1 $var2" echo $var3 x = 5 vs x=5 param="$1" datevar="$(data)" "{$foo}bar" read -p "enter here:"var
echo $var>>~/notes.txt

test expression 
[[ -e $filename ]] [[ -d $dirname ]] -n -z -h  [[ !$1 ]] [[ $userinput ]] [[ $str="user1" ]] (Spaces are important. $# script input arg count $? last command status code string compare = < > int compare -eq -ne -lt -gt ${#var} len ${#$1} help [[ if [[ ! -d $dir ]]; then if [[ $# -ne 2 ]]; then if [[ ! -d $1 ]] then
[[ $answer=[Yy] ]] [[ $answer=RegEx ]]

not expression 

if mkdir working; then if mkdir "$bindir"; then if type "$unknowncommand"; then
count_1 =$(ls -Al "dir1" | wc -l)

if type "$scriptname"; then 
exit 0
else
exit 1
fi

I/O
printf "|%20s |%20s |20%s |\n" $(ls) echo -n "no new line"
read; echo $REPLY IFS=: read a b (enter 1:2)
/dev/stdin &0 0> /dev/stdout &1 1> > /dev/stderr &2 2> /dev/null cmd_hide_error 2>/dev/null cmd_hide_output 1>/dev/null cmd_out_as_err 1>&2 cmd_out_as_err >&2 ls > logfile 2>&1 if type "$newscript" >/dev/null 2>&1; then
sum() { return $($($1+$2))} declare -i v sum 5 6 v="$?" start_with_a () { [[ $1==[aA]* ]]; if start_with_a ax;  then [[ !$1 ]]&& exit 0 error() {} >&2 isnum() { return [[ $1 =~^[0-9]+$ ]]; declare -i len="${#1}+4"
cat >>END
 function cannot return value must use v="$?" 
END
for f in "*$1"; do base=$(basename "$f" "$1") echo mv "$f ${base}$2" done mvtxt2csv txt csv touch {a..h}.txt for ((i=0;i<10 ++i)); do done
ls -l | awk '{print $5}' od -ta octal dump  ps -ax | tee acopy.txt | more
$svnV=$(svn --version --quiet) dver=$(print $svnV|sed 's/\.//g') pver=$(print $dver| cut -c1-${#pad) would be 1400

Control
while read -r; do echo $REPLY break continue done [[ $# -ne 2 ]];
case $1 in x|y|z) echo "xyz";; w) echo "" *) esac 
mkdir newDir && cd newDir mkdir existingDir || rm existingDir


declare -r const="113" declare -i p declare +i p  expression (( p=$(ls|wc -i)+10 )) declare -i t=(( ($RANDOM%10)+1 )) until(( t==3 )); do read -p $t (( t )) || continue done

declare -a ar ar=( 1 2 a b) echo ${#a[@]} ${!a[*]} count and indexes  x[0]="1" x[15]="yes" ${x[*]} ${x[@]} ${x[0]} declare -x expVar export expV2="pass into sub proc" call_subscript

substitution vs command $((..)) vs ((..)) p=$((++x)) vs ((++x))

^C+r search history
crontab -u myjobs.txt 30 00 * * * script1.sh crontab -l crontab -u jimmy -l crontab -r crontab -e
 at -f script1.sh noon tomorrow exec >log 2>errlog nohup nice longrun.sh >log & tail -f log bash -x script1 source script1 same as . script1 sleep 1

Parameter and options 
$1 ${10} $0 could be symbolic link  $@ $* $# "$@" differ "$*" for p in $@; do  script "a1 arg1" a2 a3 for p in "$@"; --"$@" positional, "@*" one arg. $@=$* no quote. do while [[ $1 ]]; do read file as 1st shift done shift 3 shift $(( OPTIND -1 ))
while getopts "b:r:s" opts; do case b) r) s) \?) script1 -r -b 5 ${OPTARG} -r stop --endofopt file.txt ":b:r:s" silence :)each "miss $OPTARG"


debugging
set -x -n -v -u -e
#!/bin/bash -x
set -x
...
set +x
$EDITOR export EDITOR=nano
#include source f1.sh = . f1.sh . .bashrc reload startup script now


Friday, February 26, 2016

bash command braindump

man -k ls /[search] help ls pwd eacho $PATH $BASH SSH 192.0.0.0 $ 1st promp > 2nd promp ls -alcat less /[search] file rm mv mkdir touch reset clear cd 'prog files' cd prog\ files ls ~/tmp/../temp/web1 ls ~ ls / cp -R docs newDir ls -R rmdir -r mv (safe -Ri -ri) ln -s ls [RegcatEx]a[RegEx] ls [abc_$]a[^abc]?.txt touch file{1,2}.{txt,jpg} cat>file1.txt ls >>file1.txt pgrep -u daemon |less grep 1978 file1.txt  cat -f 3 file1.txt | wc -l touch "note$(whoami).txt" touch "note$(date+%Y-%m-%d).txt"


find -name "*.txt" -exec grep -l Hello {} \; cat student.txt | tr Steve steve  tr \\t \; coldel.csv
ls -lS | head ls -lt |tail  tail -f Live.log ls -a |wc -l grep -v "^$" student.txt sort student.txt |uniq sort -nk2 -t\; Data.txt cut -f 3 -d\; Data.txt paste f1.txt f2.txt join f1.txt f2.txt

gedit delayed.sh & bash delayed.sh chmod u+x delayed.sh ./delayed.sh >output.txt & bg %1 ^Z bg fg ^c jobs kill %1 kill -KILL %cp ps -e | grep delayed* kill 9969 (dangerous) xkill pkill top

vi .bashrc :q ls -al $HOME |grep .bashrc find ~ -name ".prof*" alias  alias rm="rm -I" \rm alias dw="cd ~/Donwloads" echo $PS1 man bash /[search] PS1 n env echo $PATH PATH="$PATH:~/bin" >> .bashrc less text.txt v echo $SHELL chsh
sudo find -type f -name "/var/log/*.log" -print0 | xargs  -0 sudo chmod a+rw
ifconfig |grep 'inet'
set -o set -o noclober ls -l >| f1.txt history -c -r -w ls -l /etc>f.txt 2>&1 df -h
mkfifo p1 ls -l >p1 wc -l p1 mkfito /tmp/p1 ls -l !$ (last arg) ls -F !$ ls -l | tee f1.txt

Useful Command Line interface

^[A-Z][1,2]\s[a-z]{2}\b$--start-option-space-must-wordboundary-end [[ =~ ==* ]] pattern matching test [[ hello =~ h*o ]] && echo "matched" [[ hello =="h*o" ]] not a match of str
grep -i -v -E -ve '^#' -ve '^$'(case, inverse, enhance, multi expression as in e) grep '\bserver\b'
sed -i '/^#/d;/^$/d' (in-place really edit file,d=delete ; multi cmd) 

WinScp --open source Windows Ubuntu file copying
cat -vet notepad.sh show M$ dos2unix test.sh to remove
ls /proc cat /proc/version (all  process)
sudo -i switch to root, service apache2 restart, less /etc/passwd , 15G,goto, /[search]+n next PageUp/Dwn
cut -f1,3 -d':' /etc/passwd sort f1.csv (by 1st char) sort -r -k4 -t ',' -n f1.csv (revese, column 4 space and ',' deliminated) cut ...| sort...
cp/rm/mv -i -r -a (interactive, recursive, keep owner as in ls -lh) rm -rf dir1 (r+force)
ls -l lnk1= readlink lnk1 alias ls='ls --color=auto' -lt (time).
dd if=/dev/sda1 of=mbr count=1 bs=512 (disk image)
rsync -av /home/ /backup/ (keep owner, verbose, end / =dir) useradd -m bob userdel -r bob rsync -av --delete (sync deletion)
time tar -czvf etc.tgz /etc (create zip,verbose file) tar -tzf=file etc.tgz (test zip file) -xzf (expand)
find -newer -type f -delete -size +138k -exec ls -lh {} \;
find /usr/share -name '*.pdf' -maxdepth 2
cd /dev cd - // cd to prev out of dev

Process Command
dpkg -L procps which uptime uptime cat /procs/uptime (up, idle) /procs/loadavg
ps -t -l -e    sleep 180 & seconds jobs fg bg 
pgrep sleep  kill 2300 = pkill sleep killall sleep  kill -l (list all signals. e.g -9=sigkill=no-respondings)

top toggle l,t,m (load,cpu%,mem) f arrow s=sortby esc r=renice=lower priority k (invalid pid or 0 sig to esc)

vi q q! w x i : esc dw (del word), i, a (before, after) x=bsp=del I/A=begin/end of line u=undo
vi +603 /[search] f.c goto line 603 search when open,603G, 1G   b w $ ^ (back forward)
vi /etc/hosts  3,7w ip.txt (write out 3-7 words)
ln -s file1/dir1 slink1 symbolic=soft link ln file1 hlink hard link only to file

FAQ
tac = sed "1! G; h$!;d" sed -n "10p" f1.txt = head -10|tail -1 sed -n "5,$p"|sed "/MNO/s/ABC/DEF"
finger jimmy comm f1.txt f2.txt
sed -i "/^[(backslash)011(backslash)040]*$/d" f.txt oct for space/tab remove empty line.
grep -c "ABC" count line du -s jimmy disk usage summary egrep=ext cmdf3 2>f2 input to cmd out,err
awk 'END {print NR} filename' awk -F':' '{print $1} /etc/shadow' all user name. sed '100iABC' f.txt
find . -mtime -2 -exec wc -l {} \;  #must {}[space]\; mod last 2 days
umask 777 default rwx for all

Remove/Search/Replace -- param expansion
$() vs ${} ${str[op][pattern]} op=empty=> remove op=/ or // => search replace # begin % end, pattern=*?[] ##/%% #/% =>longest vs shorted in remove.
i="mytxt.txt" ${i/txt/jpg} myjpg.txt # not useful ${i/%txt/jpg} mytxt.jpg ${i//txt/jpg} myjpg.jpg
${i%txt} mytxt. ${i#*txt} .txt  ${i##*txt} empty ${i/[yx]/a} matxt.txt ${i//[yx]/a} matat.tat remove is replace with empty

default value --- v2=${v_unset:-99} echo $v2 99 v_unset=88 now echo $v2  88 end of option --  touch -a.txt failed touch -- -a.txt 


Thursday, February 25, 2016

How to think about Dynamic Programming in Matlab code

(1) Dynamic programing is a multi-path-backward-recursive algorithm, i.e. known end result=> max target today.
(2) Oil reserve with normal/enhance 10k/20k exploration => recombining binary tree=> array
(3) value function on the binary tree has known result [0,0,0,0] at the end t=4.
(4) Binary Tree would have two value function Vn/Ve and two backward calculation, price progression 45-30-40
(5) [x]=dynProg() and x(1).Vn vs x(1).Ve will tell you which branch to go. Back-calc=>Forward visualization
(6) Code is not visual enough. Visualize an algorithm is a good WPF project.

function [x]=dynProg()
% states --oil reserve
x(1).rsrv=[600000];
x(2).rsrv=[500000 400000];
x(3).rsrv=[400000 300000 200000];
x(4).rsrv=[300000 200000 100000 0];

% Normal and Enhanced Pumping
% Value Onward at t=4 is 0 due to lease expiration
x(4).Vn=[0 0 0 0];
x(4).Ve=[0 0 0 0];
x(4).V=max(x(4).Vn,x(4).Ve);

% Value
% Onward=oilPrice*policy-multiplier*policy^1/reserve+discountedValueOnward
for i=1:3
   x(3).Vn(i)=40*100000- 1*100000^2/x(3).rsrv(i)+0.9*x(4).V(i);
   x(3).Ve(i)=40*200000- 20*200000^2/x(3).rsrv(i)+0.9*x(4).V(i+1);
end
% ValueOnward= Max (Norma, Enahnced)
x(3).V=max(x(3).Vn,x(3).Ve);

for i=1:2
   x(2).Vn(i)=30*100000- 1*100000^2/x(2).rsrv(i)+0.9*x(3).V(i);
   x(2).Ve(i)=30*200000- 20*200000^2/x(2).rsrv(i)+0.9*x(3).V(i+1);
end
x(2).V=max(x(2).Vn,x(2).Ve);

x(1).Vn(1)=45*100000- 1*100000^2/x(2).rsrv(1)+0.9*x(2).V(1);
x(1).Ve(1)=45*200000- 20*200000^2/x(2).rsrv(1)+0.9*x(2).V(2);
x(1).V=max(x(1).Vn,x(1).Ve)

Some WCF messaging scenarios


Message Streaming, including REST

   <bindings>
      <webHttpBinding>
        <binding name="strmREST" transferMode="Streamed"  maxReceivedMessageSize="67108864"/>
      </webHttpBinding>
      <netTcpBinding>
        <binding name="strm2" transferMode="Streamed"  maxReceivedMessageSize="67108864"/>
      </netTcpBinding>
      <netNamedPipeBinding>
        <binding  name="strm3" transferMode="Buffered" maxReceivedMessageSize="67108864"/>
      </netNamedPipeBinding>
      <customBinding>
        <binding name="strm4">
          <namedPipeTransport transferMode="Streamed"/>
          <textMessageEncoding  messageVersion="Soap2WSAddress" />
        </binding>
      </customBinding>
    </bindings>
  </system.serviceModel>

Inject Address Header

    <services>
      <service name="Wcf_Tryout.Service1">
        <endpoint>
          <headers>
            <Member>Header1</Member>
          </headers>
        </endpoint>
      </service>
    </services>

  <system.serviceModel>
    <client>
      <endpoint address ="" binding="b1" contract="" >
        <headers>
          <Member>Header1</Member>
        </headers>
      </endpoint>
    </client>


            var b= new EndpointAddressBuilder(new EndpointAddress("tcp://localhost:8080/s"));
            AddressHeader h = AddressHeader.CreateAddressHeader("header1");
            b.Headers.Add(h);
            EndpointAddress ea= b.ToEndpointAddress();

            MessageHeader<string> mh= new MessageHeader<string>("header1");
            OperationContext.Current.OutgoingMessageHeaders.Add(mh.GetUntypedHeader("id","http://ns"));
            MessageHeaderInfo inInfo = OperationContext.Current.IncomingMessageHeaders[0];

Restful WCF Contract means WebGet/WebInvoke

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate = "users/{userName}/bookmark?tag={Tag}")]
        string GetData(string userName,string tag);

        [OperationContract]
        [WebInvoke(Method = "Post", UriTemplate = "users/{username}/bookmarks/{id}")]
        void SetData(string userName, string id, User data);
    }


 


Tuesday, February 23, 2016

Cache plan


Create Procedure SeeCacheWSize (@str NVARCHAR(4000))
as

select cp.objecttype, cp.cacheobjtype
 cp.size_in_bytes, cp.usecounts, st.text
from sys.dm_exec_cached_plans as cp
  cross apply sys.dm_exec_sql_text
  (cp.plan_handle) as st
where cp.objtype in ('adhoc','prepared')
and st.text like @str
 and (st.text not like '%syscacheobjects%'
   or st.text not like '%select%cp.objecttype%')

usage SeeCacheWSize '%m_safe%' or '%m_unsafe%'

Dynamic Management View/Function
DMV:sys.dm_exec_query_stats 
DMF:sys.dm_exec_sql_text(sql_handle)
DMF:sys.dm_exec_query_plan(plan_handle) 

Monday, February 22, 2016

SQL Server Histogram and SqlCmd non-visual view of Cache

SqlCmd command line is non-visual ( so could not see Index scan vs seek or Table Scan) so may have to use SQL Management Studio.

About statistics:
(1) Statistics have Index info, Density Vector and Histogram. sp_helpstatistics N'dbo.mytable',N'ALL'
(2) DBCC_Statistics tname, idxname can use idxname from sp_helpindex. sp_helpstatistic must use ALL or tell us no stats.
(3) Histogram tell us EQ_ROWS, RANGE_ROWS, AVG_RANGE_ROWS. It is generated by Server automatically or on demand
(4) @var used in statement => unknown to histogram and  server ends up using density vector
(5) Graphic execution plan actual vs est two => how well histogram get used good or bad.
(6) Histogram updates
        Set Auto_create_statistics on (20% + 500 changes trigger updates)
        update_statistics
        sp_update_stat, sp_auto_statistics tname, sp_create_statistics
         
SqlCmd non-visual View Cache may have a simple and quick view of how SQL Server understand your statement.

(1) @var in ad hoc batch => unknown and no plan
(2) Forced Cache plan in exec sp_executesql @execStr,"@v int", 1234
(3) select * from member where lastName like '%e%' => non parameterized unsafe, pollution of plan.

here is the stored proc to view Cache

create proc dbo.SeeCache(@str NVARCHAR(4000))
as
    select st.text,qs.execution_count,qs.query_plan_hash
    from sys.dm_exec_query_stats as [qs]
      cross apply sys.dm_exec_sql_text
       (qs.sql_handle) as st
      cross apply sys.dm_exec_query_plan
       (qs.plan_handle) as p
where st.text like @str

so SeeCache '%lastname%' would output sql text some hash pattern and how many time plan get used.

Sunday, February 21, 2016

ASP.net on Linux Ubuntu

very similar to ASP.net on Mac OS with a few new things below:

(1) Node.js binary install is zip file xz and need sudo tar -C /usr/local --strip-component 1 -xf node-v4.1-linux-x64.tar.xz 

(2) ASP.net 5/Core CLR are installed with the following command line -- differ from Mac OS where they are installed by VS Code

sudo apt-get install unzip curl

curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh

sudo apt-get install libunwind8 gettext libssl-dev libcurl4-openssl-dev zlib1g libicu-dev uuid-dev

dnvm upgrade -r coreclr

now have to install libuv:

sudo apt-get install make automake libtool curl
curl -sSL https://github.com/libuv/libuv/archive/v1.8.0.tar.gz | sudo tar zxfv - -C /usr/local/src
cd /usr/local/src/libuv-1.8.0
sudo sh autogen.sh
sudo ./configure
sudo make
sudo make install
sudo rm -rf /usr/local/src/libuv-1.8.0 && cd ~/
sudo ldconfig

(now /usr/local/lib should have all libbu files,for libux not found error do "export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY")

Node.js
 Node.js is not native to Yomen scaffolding. So native Express/Node.js scaffolding may be considered:

sudo install -g express-generator
express myWeb2
cd myWeb2
npm install
npm start     will see at http://localhost:3000

code .

for full intellisense in VS code need to install typescript defintion
sudo npm install -g tsd
tsd install node
tsd install express

the following node.js server code will have full intellisense:

var http=require("http");

var server=http.createServer(function(req,res)
{
    res.write("<html><body>hello from node.js server launched from vs code</body></html>");
    res.end();
});

server.listen(3000);

Saturday, February 20, 2016

Linux Signal as normal code execution with Handler


Ctrl-C would send SIGINT and can be capture by a handler on a fork. some other signal kill -TERM 123, kill -HUP 123, pgrep sleep, kill -INT 123

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void handler(int sigtype)
{
 printf("type: %d\n",sigtype);
}

struct sigaction make_sigaction()
{
 struct sigaction a;
 a.sa_handler=handler;
 a.sa_flags=SA_RESTART;
 a.sa_flags=0;
 sigemptyset(&a.sa_mask);

return a;
}

int main()
{
 struct sigaction a=make_sigaction();
 sigaction(SIGINT,&a,NULL);
// signal(SIGINT,SIG_IGN);  // will ignore handler ctrl+c
// signal(SIGQUIT,SIG_IGN); // ignore ctrl+backslash
 int c;
 while(1)
 {
   printf("%d\n",c++);
   sleep(1);
 }
}

Note: (1)can create a blocking call instead of a busy while above
           int p[2]; 
           pipe(p); 
           while (1) {
              read(p[0],buf,1000);  .....
           } 
      (2) sigaction with alarm and read blocking call can have a read with timeout

          SIGALRM will cause system call to return after timeout

          sigaction(SIGALRM, &action, NULL);
          alarm(3);
          int n = read(0,line,100);  // after 3 sec, no longer blocking
          alarm(0); //cxl alarm
          if(n==-1 && errno==EINTR) return -1;

First Landing on Ubuntu with Linux gcc and native command

dual boot with Windows 10 off an DVD 
sudo apt-get install gcc
gcc hw.c, ./a.out, sudo cat foo
sudo pm-hibernate

python3 hw.py
#!/usr/bin/python3
import os

fout=os.open("foo2",os.O_WRONLY|os.O_CREAT,0o644)

os.write(fout,bytes("hello Python on Lunux","utf-8"))
os.close(fout)

qt creator IDE c or C++ project but buggy for include and intellisense
gcc -H hw.c show all header files
eclipse C/C++ IDE (Mars) preference Editor/Adance check proposal will
assit intellisence, use ctrl-space to get O_WRONLY

eclipse install new software menu-> http://pydev.org/updates will install Python IDE.
note that Ubuntu has Phython 3 installed

Linux Concurrency intervening

int main()
{
 int i,j=0;
 if(fork())
  while(i++<100)   printf("parent %d\n ",getpid());
 else
  while(j++<100) printf("child %d\n",getpid());
}

Linux Concurrency synchronize by signal
header files stdio.h, stdlib.h 
must include string.h for strsignal or segmentation fault for strsignal(WTERMSIG(status))
int main()
{
 int status;
 if(fork())
 {
      wait(&status);
      if(WIFEXITED(status))
        printf("exit status %d\n ",WEXITSTATUS(status));
      if(WIFSIGNALED(status))
       printf("signal status %d: %s\n",WTERMSIG(status),strsignal(WTERMSIG(status)));  
 }
 else
 {
   *(int*)0=99;  // segmentation fault
   exit(3);
 }
}


Pipe:

mkfifo /tmp/jqd
ls -l /tmp
cat /tmp/jqd (blocking) until another terminal date> /tmp/jqd

Wednesday, February 17, 2016

Mac OS Bash Command line building ASP.Net

From nodejs.org install, we can get a series of command line tools for ASP.net 5 development on Mac OS.
(non command install or info: get.asp.net (ASP.Net 5 and .netCore), nodejs.org. code.visualstudio.net, omnisharp)

Here is the list of command from Mac OS Terminal (Bash) to install and build ASP.net 5.

sudo npm install -g generator-aspnet
sudo npm install -g yo   (yoman scafalting)

yo aspnet mywebapp 
cd [mywebapp]
dnu restore
dun build
dnx web
http://localhost:5000


sudo chown jimmy ~/.bash_profile
open ~/.bash_profile to add function code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $*; }

yo aspnet:WebApiController myAppController
http://localhost:5000/api/myapp

Code . 

Note that there is a Docker file in the project so really deployment to server must have a container to host ASP.Net 5 with .netCore

Tuesday, February 16, 2016

DataTemplate templating per data item or type


(1) As Content of ContentControl through data type binding

    <Window.Resources>
        <DataTemplate DataType="{x:Type local:BondViewModel}">
            <TextBlock Text="UserControl is a ContentControl"/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <UserControl Content="{Binding .}">
            
        </UserControl>
    </Grid>

DataContext = new BondViewModel();

(2) As ContentTemplate of ContentControl
    <ContentControl Template="{ControlTemplate->ContentPresenter}" ContentTemplate="{DataTemplate}"

(3) As ItemTemplate of ItemsControl
   <ItemsControl Template="{ControlTemplate->ContentPresenter}" ItemTemplate="{DataTemplate}"
         ItemsPanel="{ItemPanelTemplate->WrapPanel}"

Monday, February 15, 2016

DevOps and DevCloud: Docker or Distributed with Cloud

Developer has evolved to code against Hardware, OS, VM and now Operation Environment (DevOps and DevCloud).
(1) Cloud: SaaS, PaaS, IaaS. Ops: Chef=Aims Deployment cookbook, xml desc, Docker= carry Ops cross platform, ClickOnce, Store App unit.
(2) Four Cloud Design Patterns:
        RPC (sync) ----SalesForce.com, AWS VPC, AWS Bus, BizTalk/WCF, Mule Studio,
        Async( Messaging)--- AWS SQS (simple queue), Azure Notification Hub, Mule ESB/Mule Studio, WebStorm/Node.js(best js IDE)
        Shared DB (DB in cloud) -- Azure DB, Azure Table Storage AWS RDS, AWS S3 (flat file DB). 
        Replication (ETL) --SSIS, Informatica Cloud (ETL Meta and Designer), AWS Data Pipeline, MS SQL Data Sync Agent.
(3) Docker ---Container on Linux. Now Windows Server 2016 support Docker for deploy Windows app.     
              ASP.net 5 can be deployed to Docker Linux using VS 2015 ASP.net 5 Docker Extension

(4) Windows Server 2016 has Server Container=Docker, which runs on one OS as container always does. But is still have Hyper-V container =VM, which run an OS each.

(5) Docker can be install on window 10 --- Install Docket Toolbox and it will run inside Oracle VirtualBox. And "docker run -it Ubuntu bash" will pull down bash image. the VM is a light weight Linux distribution called boot2docker. And there are MinGW64 inside.


Sunday, February 14, 2016

ActiveMQ C++/Win32 Listener

ActiveMQ download includes some CPP files to be build by gcc. It would be interesting to use VS 2015 C++ to build a listener.

(1) http://activemq.apache.org download to c:\ActiveMQ has bin\x64 install as service. Need java 64-bit using java -d64 -version to check.
(2) Web page management tools: http://localhost:8161 admin/admin
(3) need to compile C# sample, Nuget Apache NMS two download to run publisher and Listener. C++ code only need publisher to run pushing data out
(4) VC++ in VS2015 need a Win32 Console project. I can only get Win32 Config+Tageting Win8.1 working. Win10, X86 or X64 have lib missing issues.
(5) there are plenty of header include, Lib include and lib dependencies as state below:
     VC++ header include
         C:\working\apr-1.5.2\include;C:\working\activemq-cpp-library-3.9.2\src\main;
     VC++ Lib directory
         C:\working\apr-1.5.2\LibD;C:\working\activemq-cpp-library-3.9.2\vs2010-build\Win32\Debug\activemq-cpp
     Link/Input/Additional Dependencies
         C:\working\activemq-cpp-library-3.9.2\vs2010-build\Win32\Debug\libactivemq-cpp.lib;C:\working\apr-1.5.2\LibD\apr-1.lib;C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\WSock32.lib;C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\WS2_32.lib;C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\RpcRT4.lib;

(6) as Shown in (5), there are apr (apache portable runtime), activeMQ cpp to be build from source. one is old C++/C code and one is VS 2010 code, while the latter need to include header of apr, there are not much lib or dependencies struggle.

(7) a lot of time spend on unresolved symbols, as shown in above reference to Platform SDK, There are some file by file linker dependencies like WSock32.lib,WS3_32.lib. really drive me crazy.

Here is the quite simple code that can run against ActiveMQ C# Publisher sample for tcp and failover protocol.


#pragma once
#include "stdafx.h"
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/core/ActiveMQConnection.h>
#include <activemq/library/ActiveMQCPP.h>
#include <decaf/lang/Integer.h>
#include <decaf/lang/System.h>
#include <activemq/util/Config.h>
#include <cms/Connection.h>
#include <cms/Session.h>
#include <cms/Destination.h>
#include <cms/MessageProducer.h>
#include <cms/TextMessage.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace activemq;
using namespace activemq::core;
using namespace decaf::lang;
using namespace cms;
using namespace std;
int main()
{
 activemq::library::ActiveMQCPP::initializeLibrary();

 {
  ActiveMQConnectionFactory factory;
  factory.setBrokerURI(std::string("tcp://localhost:61616"));
  // factory.setBrokerURI(std::string("failover://(tcp://localhost:61616,tcp://localhost:61616)"));

  std::auto_ptr<Connection> connection(factory.createConnection("admin", "password"));

  std::auto_ptr<Session> session(connection->createSession());
  std::auto_ptr<Destination> dest(session->createTopic("event"));
  std::auto_ptr<MessageConsumer> consumer(session->createConsumer(dest.get()));

  connection->start();

  long long start = System::currentTimeMillis();
  long long count = 0;
  while (true) {

   std::auto_ptr<Message> message(consumer->receive());

   const TextMessage* txtMsg = dynamic_cast<const TextMessage*>(message.get());

   if (txtMsg != NULL) {
    std::string body = txtMsg->getText();
    if (body == "SHUTDOWN") {
      cout << "done " << endl;
     break;
    }
    else
    {
     count++;
     cout << "received message " << body << " " << count << endl;
    }

   }
  }
  getchar();
 }

 activemq::library::ActiveMQCPP::shutdownLibrary();
    return 0;
}

Saturday, February 13, 2016

Think in PowerShell code

(1) PS has coding construct such as range, dictionary, array, function, module, for, switch,if
(2) Pipeline can do foreach, where, similarly cmdletBinding().
(3) help and get-command are similar but the latter can filter down to specific ones. 
(4) internal variable cannot get from help neither member. so get-variable and get-member would be needed.
$Subnet="192.168.0."
200..210|where{Test-connection "$Subnet$_" -count 1 -quiet}|foreach{"$subnet$_"}  #where taking in True/False by _quiet
100..120|foreach{ Test-Connection "192.168.0.$_" -count 1} help Test-Connection -wh show-command Test-Connection
100..120 | where { test-Connection "192.168.$_" -quiet } #show 100,101, 109 only success ones

Function
{
   [CmdletBinding()]
   Param (
      [parameter(Mandatory=$true, helpmessage='hel']
      $subnet,
      [int]$start
   )
   write-verbose "..." # cmdletbinding allows -Verbose common param and code inside ps1.
}

Function PipeEnabledFun()
{
  begin {}
  process { $ret=$_;}
  end {return $ret}

}

Several function made module as psm1 Import-Module.

help export, select,ExecutionPolicy.

Get-variable, $PSVersion, etc.

help get-command (better than help), get-command *counter*

Reflection:  get-service | get-member
$dict={$v="v1"; $v2="v2"}
$array="s1","s2"
if($v -eq "v") 

switch($v)
{
  41 {"test"; break;}
  default {}
}

foreach($file in get-childItem)
{
  if ($file.Name -like "test")
}

$ScriptBlock ={test-Connection}
&$scriptblock;

Friday, February 12, 2016

writing WPF/MVVM ViewModel in C++/CLI

WPF/MVVM fully supported in C# and libraries such as Prism. It is an interesting attempt to use C++/CLI hand-code VM, so that low latency C++ code can send data directly to VM for databinding.

note that C++/CLI only useful for interop, C# is better for CLR. so <msclr/marshal> using namespace msclr::interop; marshal_context ctx; ctx.marshal_as<LPCSTR>() come handy

#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Reactive;
using namespace System::Reactive::Linq;

namespace JQD{
namespace ViewModels
{
 public ref class MarketWatchViewModel : INotifyPropertyChanged  {
  public:
   MarketWatchViewModel::MarketWatchViewModel()
   {
    IObservable<__int64>^ obs = Observable::Interval(TimeSpan::FromTicks(1)); //1 tick= 100 nano sec, 1 microsecond=1000 ns=1/1000 ms
    Action<__int64>^ a = gcnew Action<__int64>(this, &JQD::ViewModels::MarketWatchViewModel::DoFakeData);
    System::IObserver<__int64>^ obsr = Observer::Create<__int64>(a);
    obs->Subscribe(obsr);
   }
  
   void DoFakeData(__int64 i)
   {
    double x = 1.234;
    double* ptr = &x;
    Price = *ptr*i; // involve native C++ just to test it work still for WPF
   }

   virtual event PropertyChangedEventHandler^ PropertyChanged;
   void NotifyPropertyChanged(String^ propertyName)
   {
    PropertyChanged(this, gcnew PropertyChangedEventArgs(propertyName));
   }

   property String^ Id
   {
    String^ get() { return _id; }
    void set(String^ value) {
     if (value == _id) return;
     NotifyPropertyChanged("Id");
     _id = value;
    }
   }

   property double Price
   {
    double get() { return _price; }
    void set(double value) {
     if (value == _price) return;
     NotifyPropertyChanged("Price");
     _price = value;
   }

  private:
   String^ _id;
   double _price;
  };

}
}

namespace WpfTester
{

public partial class MainWindow : Window
{
 private MarketWatchViewModel vm = new MarketWatchViewModel();

 public MainWindow()
 {
  InitializeComponent();
  DataContext = vm;
 }
}
}

<Window x:Class="WpfTester.MainWindow"
..>
<Grid>
 <Label Content="{Binding Price}" HorizontalAlignment="Left" Height="59" Margin="94,142,0,0" VerticalAlignment="Top" Width="234"/>
</Grid>
</Window>

Thursday, February 11, 2016

C++ <functional> concrete monad


(1)commonly used generic functor greater<>(), lesser<>(), plus<>(), multiplies<>();
(2) bind() is to be replaced by lambda
(3) lambda is Callable Object, similar to c function pointer but extended to have pmd/pmf concept=pointer to member data/reference
(4) invoke(Callable,value) is template+ functional generic code execution.
(5) from monad functional space to classical code, decay_t(result_of_t(callable(invoke)))) would do it.
(6) reference_wrapper convert value semetic in monard
(7) function is a monad recorder to playback


#include "stdafx.h"
#include <functional>
#include <vector>
#include <algorithm>
#include <iostream>
#include <random>

using namespace std;

void tryout_result_of();
void tryout_reference_wrapper();
void tryout_function();

template <typename Range,typename Callable>
void transform_print(const Range& r, Callable c)  // lambda is just a callable object from unamed type
{
 for (const auto&e :r)
 {
  cout << invoke(c, e) << endl;
 }
}

//pmd =point to member data, hard to optimize, pmf= pointer to member reference.
int main()
{
 vector<int> vint{ 11,28,3,14,5,96 };
 vector<string> vstr{ "test","jpmc","password","bankd","hou","pick peach los hand" };

 sort(vint.begin(), vint.end(), greater<>());  //functor generic type
 for (const auto&e : vint) cout << e << endl;

 cout << count_if(vint.begin(), vint.end(), bind(less<>(), placeholders::_1, 9))<<endl;   // avoid bind use lambda as below, bind has pmd

 cout << count_if(vint.begin(), vint.end(), [](const auto& p) {return p < 5; }) << endl;

 vector<pair<int, int>> vpint{ { 1,2 }, { 9,3 }, { 11, 55 } };
 transform_print(vpint, [](const auto& p) {return p.first*p.second; });  // lambda is a callable object better than bind and should replace it.
 transform_print(vpint, &pair<int, int>::second);

 tryout_result_of();
 tryout_reference_wrapper();
 tryout_function();

 getchar();
    return 0;
}


template<typename T, typename Callable>
auto transform_sort(const vector<T>& v, Callable c)
{
 vector<decay_t<result_of_t<Callable&(const T&)>>> ret;
 for (const T& t : v)
 {
  auto i = invoke(c, t);
  int ii = 0;
  ret.push_back(i);
 }
 sort(ret.begin(), ret.end());
 return ret;
}

void tryout_result_of()
{
 const vector<string> v{ "test","functional","result_of","not really functional" };
 auto lambda = [](const string& s) {return s.size(); };

 cout << endl;
 for (auto& e : transform_sort(v, lambda))
 {
  cout <<e<< endl;
 }

}

void tryout_reference_wrapper()
{
 vector<int> v(8);
 auto b = v.begin(); auto e = v.end();
 typedef uniform_int_distribution<int> Dist;
 auto d20 = [urng = mt19937(3344), dist = Dist(1, 20)]() mutable { return dist(urng); };

 generate(b, e, d20);
 for (auto a : v) cout << a << ",";
 cout << endl;
 generate(b, e, d20);   // same set of int
 generate(b, e, ref(d20));
 generate(b, e, ref(d20));  // different set due to reference_wrapper
}

int sum_square(int x, int y) { return x*x + y*y; }
void tryout_function()
{
 vector<function<int(int, int)>> v;  // record a sequence of action for later playback, only signature matter can be pmd,pmf, regular function
 v.emplace_back(plus<>());
 v.emplace_back(multiplies<>());
 v.emplace_back(&sum_square);  // can ommit &
 for (int i = 10; i <= 1000; i *= 10)
  v.emplace_back([i](int x, int y) {return i*x + y; });

 for (const auto f : v) cout << f(1, 2) << endl;
}


Sunday, February 7, 2016

Attempting Lock Free Stack using <atomic>

 (1) atomic has compare_exchange for lock free code
 (2) to be joinable, thread ctor must take in a func. But could not get it to run push/pop directly. instead run in pusher/poper ctor.

#include "stdafx.h"
#include <atomic>
#include <thread>
#include<vector>

using namespace std;

struct Node {
 Node *next;
};

enum {
 NumNode = 20,
 NumThread = 4
};


class Stack {
 atomic<Node*> head;
public:
 Stack() : head(0) {}
 void push(Node *n) {
  do {
   n->next = head;
  } while (!head.compare_exchange_strong(n->next, n));
 }
 Node *pop() {
  Node *n;
  do {
   n = head;
   if (!n)
    return 0;
  } while (!head.compare_exchange_strong(n, n->next));
  return n;
 }
};

class runnerPop : public thread
{
public:
 Stack* stack;
 runnerPop(Stack* s) : stack(s), thread([](){}) {
  runPop();
 }
 void runPop()
 {
  auto n = stack->pop();
  while (n)
  {
   delete n;
   n = stack->pop();
  }
 }
};

class runnerPush : public thread
{
public:
 Stack* stack;
 runnerPush(Stack* s) : stack(s), thread() {
  runPush();   // cannot get thread(runnerPush::runPush,this) to work, access violation. have to run after ctor
 }

 void runPush()
 {
  for (int i = 0; i < NumNode / NumThread; i++)
  {
   stack->push(new Node());
  }
 }
};

int main()
{
 Stack stack;
 
 {
  vector<runnerPush> vPush;
  for (int t = 0; t < NumThread; t++) {
   vPush.push_back(runnerPush(&stack));
  }
   for (auto& v : vPush)
   {
       v.join(); // this will blow up since thread default ctor is used, not joinalbe
   }
 }

 {

  vector<runnerPop> vPop;
  
   for (int t = 0; t < NumThread; t++) {
    vPop.push_back(&stack);
   }
    for (auto& v : vPop)
    {
       v.join();  // this is joinable, thread ctor has empty lambda
    }
  
 }
    return 0;
}



C++ Safe Pointer coding model

There is a chance C++ can overtaken C# as high level language with better performance, full control and safety. Specifically, C++11/14 allocate memory through make_share and make_unique and end up tracking ownership to avoid dangling pointer.

#include "stdafx.h"
#include <memory>
#include <iostream>
#include <vector>

 auto sPtr = make_shared<int>(88);
 int* p = sPtr.get();

 sPtr = make_shared<int>(99); // Error: not allow to repoint.
 int* p1 = sPtr.get();
 *p1 = 33;
 cout << *p<< endl;

 const auto& locPtr = makeTempPtr(); //*makeTempPtr will not work must use local Ptr to save i.e. make_unique can not change owner due to ~
 int* p3 = locPtr.get();
 *p3 = 55;
 cout << *p3 << endl;


unique_ptr<int> makeTempPtr()
{
 return make_unique<int>();
}

Also vector<int> modification can be tracked by code analysis:

 auto sv = make_shared<vector<int>>(100);
 shared_ptr<vector<int>>* sv2 = &sv;
 vector<int>* pVec = &*sv;
 int* p2 = &(*sv)[0];

 // the promise is the following two lines should stopped by Code Analysis tool
 pVec->push_back(42);
 sv2->reset();

 (*pVec)[0] = 22;
 *p2 = 12;
 cout << (*pVec)[0] <<" "<< *p2<<endl;

Saturday, February 6, 2016

Visualize async threading

For the following simple async-await code, DotTrace 10 shows code runs on main thread before Task ask thread pool to execute. So async is single thread with suspension.
This can also be seen in Concurrency Visualizer in VS 2015 as an VS Extension, where Begin wait in a separate swing lance but same thread id. Also execution stack is actually on main thread

    class Program
    {
        static void Main(string[] args)
        {
            (new Program()).Test();
            Console.ReadLine();
        }

        async void Test()
        {
          int i=  await LongRunCodeTask();
            Console.WriteLine("Asyn Call Return: " + i);
        }

        private Task<int> LongRunCodeTask()
        {
            return Task<int>.Run(() =>
            {
                Thread.Sleep(3000);
                return 99;
            });
        }
    }



CAS Compare And Swap Lock Free code pattern


Lock free changes to a global variable int _i can be achieved through <qatomic.h> QtAtomicInt and QAtomicPointer 

int _i=9;
void cas()
{
   int comp=_i;
    QAtomicInt atm(_i);
    while(! atm.testAndSetAcquire(comp,99,_i)){ comp=_i;}
    qDebug()<<comp<<" "<<atm<<"  "<<_i<<endl;
}

Note the single instruction must be inside a loop to ensure compare equal,then atomic update. and if not equal then update comp first.

Acquire and Release (Ordered=A+R) build a Memory Barriers so that CPU and optimizer cannot move instruction up or down out of the code block:

QAtomicPointer<<int> p;

p.fetchAnd?Acquire(&_i);
...//  inside Memory Barrier
p.fetchAnd?Rlease(&_i);

?=Add or Store.