piątek, 2 sierpnia 2013

Programming tech talks

  1. John Carmack's QuakeCon 2011 Keynote 

  2. John Carmack's QuakeCon 2012 Keynote

  3. John Carmack's QuakeCon 2013 Keynote

    • functional programming
    • strong static type check
    • http://www.pcper.com/reviews/Editorial/John-Carmack-Keynote-Quakecon-2013
  4. GoingNative 2012

  5. GoingNative 2013

    1. Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11, and C++14

    2. C++ Seasoning. (by Sean Parent)

      No Raw Loops
      • Difficult to reason about and difficult to prove post conditions
      • Error prone and likely to fail under non-obvious conditions
      • Introduce non-obvious performance problems
      • Complicates reasoning about the surrounding cod

      Alternatives to Raw Loops
      • Use an existing algorithm
      • Prefer standard algorithms if available
      No Raw Synchronization Primitives
      • You Will Likely Get It Wrong
      • They don't scale (Amdahl’s Law)
      No Raw Pointers
      • Prefer value semantic to reference semantic
    3. Writing Quick Code in C++, Quickly (by Andrei Alexandrescu)

      Intuition
      • Ignores aspects of a complex reality
      • Makes narrow/obsolete/wrong assumptions
      • “Fewer instructions = faster code”
      • “Data is faster than computation”
      • “Computation is faster than data”
      • The only good intuition: “I should time this.”

      Measuring gives you a leg up on experts who don’t need to measure

      Data Layout

      • Generally: small is fast
      • First cache line of an object is where it’s at
      • Sort member variables by hotness, descending
      Prefer zero to all other constants

      Returning containers by value worse than appending

    4. Don’t Help the Compiler (by Stephan T. Lavavej)

      Don't return by const value
      • Inhibits move semantics, doesn't achieve anything useful
      Don't move() when returning local X by value X

      • The NRVO and move semantics are designed to work together
      • NRVO applicable - direct construction is optimal
      • NRVO inapplicable - move semantics is efficient
      Don't return by rvalue reference
      • For experts only, extremely rare
      • Even the Standardization Committee got burned
      • Valid examples: forward, move, declval, get(tuple&&)
      Rely on template argument deduction
      • You control its inputs - the function arguments
      • Change their types/value categories to affect the output
      Avoid explicit template arguments, unless required
      • Required: forward<T>(t), make_shared<T>(a, b, c)
      • Wrong: make_shared<T, A, B, C>(a, b, c)
    5. Keynote: Herb Sutter - One C++
    6. An Effective C++11/14 Sampler (by Scott Meyers)

      Understand std::move and std::forward
      • std::move doesn't move
      • std::forward doesn't orward
      • neither generates code
      • they are simply casts
      • std::move unconditionally casts to rvalue (rvalue_cast)
      • std::forward conditionally casts to rvalue
      Declare functions noexcept whenever possible
      • fun() noexcept - more optimisation possibilities
      • fun() throw() - fewer optimisation possibilities
      • fun() - fewer optimisation possibilities
      • Some code may replace copying only with non-throwing moves
      • noexcept is part of function interface and client may depend on it. Don't use it only because current implementation allows it
      • noexcept is an operator, a bool value evaluated during compilation. Allows conditionally noexcept functions.
    7. The Care and Feeding of C++’s Dragons (by Chandler Carruth)

      Complexity
      • "You can solve every problem with another level of indirection, except for the problem of too many levels of indirection"
      • Complexity: the source and the solution to all programming problems
      • The cost of complexity is exponential.
      • Clever is not a Compliment!
      LLVM Sanitizers:
      • Compiler instrumentation dynamic analysis
      • Address Sanitizer, Thread Sanitizer, Memory Sanitizer, Undefined Behavior Sanitizer
      • Based on shadow memory, can’t be combined
      • Dynamic analysis onlyworks if you test your code!
    8. rand() Considered Harmful (by Stephan T. Lavavej)

      Uniform Random Number Generators
      • random_device
      • mt19937

      Distributions
      • uniform_int_distribution
    9. Inheritance Is The Base Class of Evil (by Sean Parent)

      Hide polymorphic inheritance from user of your API
      • There are no polymorphic types, only a polymorphic use of similar types
      • More flexible, Non-intrusive design doesn’t require class wrappers
      • More efficient,  Polymorphism is only paid for when needed
      • Less error prone,  Client doesn’t do any heap allocation, worry about object ownership or lifetimes
      • Exception and thread safe
  6. Exception-Safe Coding in C++ (Jon Kalb, Part 1, Part 2)

  7. The Problem with Time & Timezones - Computerphile

    • Leap second
    • Unix time
    • UTC
    • Astronomic time
  8. Linus Torvalds on git

  9. Unicode in C++ by James McNellis

    • UTF8, UTF-16, UTF-32
    • dynamic composition (Multiple representation)
      A(U+0041) + Umlaut(U+0308)=Ä
    • code unit vs. code point
    • What is length ? (Number of bytes, number of code units, number of code points)
    • Four normalisation forms
      NFC: Canonical Composed
      NFD: Canonical Decomposed
      NFKC: Compatibility Composed
      NFKD: Compatibility Decomposed
  10. Scott Meyers: Better Software — No Matter What

    1. 2\5
      • Inconsistency
    2. 3\5
      • Static analyse
      • Code review
      • Keyhole problem (on arbitrary restrictions - fixed size windows,fixed size hard coded in software)
    3. 4\5
    4. 5\5
      • Retrospectives !
  11. Plain threads are the 'GOTO' of today's computing (by  Hartmut Kaiser) Slides

    • Amdahl’s Law (Strong Scaling)
    • HPX - A General Purpose Runtime System for Applications of Any Scale
  12. CppCon 2014: The Philosophy of Google's C++ Code by Titus Winters

    • Optimize for reader not for the writer
    • Value the standard, but not idolize it
    • Be consistent
    • Avoid constructs that are dangerous or surprising
    • Avoid tricky and hard to maintain constructs
    • Don't use non-const references
    • Don't use exceptions
  13. Goals for Better Code - Implement Complete Types by Sean Parent

    • Regular type
    • Sometimes the most efficient basis operations are unsafe (violating class invariants)
  • The Silver Bullet Syndrome by Hadi Harir

    • There is no silver bullet (CORBA, COM, DCOM, WCF, node.js, J2EE, microservices, NoSQL)
    • Don't do "hype oriented programming)
    • Consider technology stability
    • Consider if it's proven technology
    • x86 mov is turing complete
    • MoVfuscator - compiler which compiles BrainFuck into mov only x86 assembly. Combined with compiler that compiles Basic into BF it gives Basic to movs compiler!
    • MoVfuscator2.0 - c compiler !!
    • x86 mov is turing complete
    • MoVfuscator - compiler which compiles BrainFuck into mov only x86 assembly. Combined with compiler that compiles Basic into BF it gives Basic to movs compiler!
    • MoVfuscator2.0 - c compiler !!

14. code::dive 2016 conference

15. cppCon 2016 conference

16. Rich Hickey


    • prefer simple over easy
    • data,values, functions are simple
    • declarative data manipulations
    • queues
    • transactions
    • avaoid incidental complexity (abything which is not required by the user0
    • Simplicity is the ultimate sophistication (Leonardo da Vinci)
  • Spec-ulatiion  by Rich Hickey
    • on how to manage software dependencies
    • SemVer 2 is broken - Minor and Patch components are irrelevant. Major component change is basically a library name change
    • Better would be to use time based versioning instead of SemVer2 

17 Pushing C# to the limit - Joe Albahari

  • very fast in-memory inter-process pipes
  • simple yet powerful remoting (10x faster that .net remoting)

18 7 Habits for Success (as explained by the ex-google tech lead)


  • Take Ownership - stop blaming other people, stop blaming you your boss, your teammates, your family or friends. If the project fails even if you did your part perfectly, it is also your failure. It is your responsibility, to make project to succeed. 
  • Accept Failure - do not pretend to be better than anyone else - you are not a superhuman - everyone make mistakes
  • What you do at 8pm matters - what do you do in your free time defines your future. If you watch TV you will be watching TV, if you learn new things you will likely use that knowledge.
  • Success is a lonely road, while failure is a crowded highway
  • The last 10% is the hardest. Finish it - when project is not finished it will not have any impact
  • Know why you want it - internal motivation is a key to be successful
  • Continuously learn 


C++ programming style

  1. No naked pointers

    • Keep them inside functions and classes
    • Keep arrays out of interfaces (prefer containers)
    • Pointers are implementation-level artifacts
    • A pointer in a function should not represent ownership
    • Always consider std::unique_ptr and sometimes std::shared_ptr
  2. No naked new or delete

    • They belong in implementations and as arguments to resource handles
  3. Return objects “by-value” (using move rather than copy)

    • Don’t fiddle with pointer, references, or reference arguments for

references:

niedziela, 19 maja 2013

Consistent Overhead Byte Stuffing (@Wiki)

Consistent Overhead Byte Stuffing

From Wikipedia, the free encyclopedia
Consistent Overhead Byte Stuffing (COBS) is an algorithm for encoding data bytes that results in efficient, reliable, unambiguous packet framing regardless of packet content, thus making it easy for receiving applications to recover from malformed packets.
Byte stuffing is a process that transforms a sequence of data bytes that may contain 'illegal' or 'reserved' values into a potentially longer sequence that contains no occurrences of those values. The extra length of the transformed sequence is typically referred to as the overhead of the algorithm. The COBS algorithm tightly bounds the worst case overhead, limiting it to no more than one byte in 254. The algorithm is computationally inexpensive and its average overhead is low compared to other unambiguous framing algorithms.[1]

Contents

Packet framing and stuffing

When packet data is sent over any serial medium, a protocol is needed by which to demarcate packet boundaries. This is done by using a special bit-sequence or character value to indicate where the boundaries between packets fall. Data stuffing is the process that transforms the packet data before transmission to eliminate any accidental occurrences of that special framing marker, so that when the receiver detects the marker, it knows, without any ambiguity, that it does indeed indicate a boundary between packets.
COBS takes an input consisting of bytes in the range [0,255] and produces an output consisting of bytes only in the range [1,255]. Having eliminated all zero bytes from the data, a zero byte can now be used unambiguously to mark boundaries between packets. This allows the receiver to synchronize reliably with the beginning of the next packet, even after an error. It also allows new listeners, which might join a broadcast stream at any time, to reliably detect the beginning of the first complete packet in the received byte stream.
With COBS, all packets up to 254 bytes in length are encoded with an overhead of exactly one byte. For packets over 254 bytes in length the overhead is at most one byte for every 254 bytes of packet data. The maximum overhead is therefore roughly 0.4% of the packet size, rounded up to a whole number of bytes. COBS encoding has low overhead (on average 0.23% of the packet size, rounded up to a whole number of bytes) and furthermore, for packets of any given length, the amount of overhead is virtually constant, regardless of the packet contents.

Zero Pair Elimination

An optimization that can reduce overhead for common payloads which contain pairs of zero bytes is to reduce the maximum encodable sequence length, freeing some codes to encode sequences terminated by pairs of zeros. In this case, bytes in the range [1,223] have the same meaning in as in the normal mode, the code 224 is used to encode a sequence of 223 bytes with no zero termination, and the remaining codes [225,255] encode sequences of length [1,30] terminated by a pair of zero bytes. This variation can achieve negative overhead (compression) for some sequences however it does complicate the en/decoding process.

Packet format

COBS encodes the input data as a series of variable length blocks. Each block, which may contain from 1 to 255 bytes, begins with a single byte that specifies the number of bytes in the block (including the length byte).
When decoding, a zero byte is appended to the decoded output after each block. As a special case, no zero is added after a block which begins with 0xFF.
Example encodings (block contents marked up in bold):

Plaintext Encoded with COBS
1. 0x00 0x01 0x01
2. 0x11 0x22 0x00 0x33 0x03 0x11 0x22 0x02 0x33
3. 0x11 0x00 0x00 0x00 0x02 0x11 0x01 0x01 0x01
4. 0x01 0x02 ... 0xFF 0xFF 0x01 0x02 ... 0xFE 0x02 0xFF
There exists one complication in this format: in the case 2. above, an extra 0x00 appears at the end of the decoded output. The only way to encode a block that does not end in zero is for it to have 254 bytes of contents, but the last block may be shorter than that. To solve this issue, a single trailing zero, if present, is removed by the decoder. If the real plaintext ends in a zero, an additional zero is added after it.

Implementation

/*
 * StuffData byte stuffs "length" bytes of
 * data at the location pointed to by "ptr",
 * writing the output to the location pointed
 * to by "dst".
 */
 
#define FinishBlock(X) (*code_ptr = (X), code_ptr = dst++, code = 0x01)
 
void StuffData(const unsigned char *ptr,
unsigned long length, unsigned char *dst)
{
  const unsigned char *end = ptr + length;
  unsigned char *code_ptr = dst++;
  unsigned char code = 0x01;
 
  while (ptr < end)
  {
    if (*ptr == 0)
      FinishBlock(code);
    else
    {
      *dst++ = *ptr;
      code++;
      if (code == 0xFF)
        FinishBlock(code);
    }
    ptr++;
  }
 
  FinishBlock(code);
}
 
/*
 * UnStuffData decodes "length" bytes of
 * data at the location pointed to by "ptr",
 * writing the output to the location pointed
 * to by "dst".
 */
 
void UnStuffData(const unsigned char *ptr,
unsigned long length, unsigned char *dst)
{
  const unsigned char *end = ptr + length;
  while (ptr < end)
  {
    int i, code = *ptr++;
    for (i=1; i<code; i++)
      *dst++ = *ptr++;
    if (code < 0xFF)
      *dst++ = 0;
  }
}

References

^ Cheshire, Stuart; Baker, Mary. "Consistent Overhead Byte Stuffing". ACM. Retrieved November 23, 2010.

środa, 1 maja 2013

Booting LINUX form Live USB

  1. Use YUMI to prepare USB stick (http://www.pendrivelinux.com/yumi-multiboot-usb-creator/)
  2. Assure you are using FAT (not FAT32) file system. For some motherboards FAT32 doesn't work. (on windows: format A: /fs:FAT)
  3. Some kernels require boot option: mem=4gb

piątek, 29 marca 2013

Collecting User-Mode Dumps


http://msdn.microsoft.com/en-us/library/windows/desktop/bb787181%28v=vs.85%29.aspx
http://blogs.microsoft.co.il/blogs/sasha/archive/2009/10/19/configuring-automatic-crash-dumps.aspx

Starting with Windows Server 2008 and Windows Vista with Service Pack 1 (SP1), Windows Error Reporting (WER) can be configured so that full user-mode dumps are collected and stored locally after a user-mode application crashes. Applications that do their own custom crash reporting, including .NET applications, are not supported by this feature.
This feature is not enabled by default. Enabling the feature requires administrator privileges. To enable and configure the feature, use the following registry values under the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps key.
ValueDescriptionTypeDefault value
DumpFolderThe path where the dump files are to be stored. If you do not use the default path, then make sure that the folder contains ACLs that allow the crashing process to write data to the folder. For service crashes, the dump is written to service specific profile folders depending on the service account used. For example, the profile folder for System services is %WINDIR%\System32\Config\SystemProfile. For Network and Local Services, the folder is %WINDIR%\ServiceProfiles.REG_EXPAND_SZ%LOCALAPPDATA%\CrashDumps
DumpCountThe maximum number of dump files in the folder. When the maximum value is exceeded, the oldest dump file in the folder will be replaced with the new dump file.REG_DWORD10
DumpTypeSpecify one of the following dump types:
  • 0: Custom dump
  • 1: Mini dump
  • 2: Full dump
REG_DWORD1
CustomDumpFlagsThe custom dump options to be used. This value is used only when DumpType is set to 0.
The options are a bitwise combination of the MINIDUMP_TYPE enumeration values.
REG_DWORDMiniDumpWithDataSegs | MiniDumpWithUnloadedModules | MiniDumpWithProcessThreadData.

These registry values represent the global settings. You can also provide per-application settings that override the global settings. To create a per-application setting, create a new key for your application under HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\Windows Error Reporting\LocalDumps (for example, HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\Windows Error Reporting\LocalDumps\MyApplication.exe). Add your dump settings under the MyApplication.exe key. If your application crashes, WER will first read the global settings, and then will override any of the settings with your application-specific settings.
After an application crashes and prior to its termination, the system will check the registry settings to determine whether a local dump is to be collected. After the dump collection has completed, the application will be allowed to terminate normally. If the application supports recovery, the local dump is collected before the recovery callback is called.
These dumps are configured and controlled independently of the rest of the WER infrastructure. You can make use of the local dump collection even if WER is disabled or if the user cancels WER reporting. The local dump can be different than the dump sent to Microsoft.


To have it working on my Windows 7 x64 I had to set auto="0" in:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\Windows NT\CurrentVersion\AeDebug