2011年09月22日 ARMedslack(linux-2.6.36.4) for zaurus SL-C860 でオーバークロック
_ ARMedslack(linux-2.6.36.4) for zaurus SL-C860 でオーバークロック
してみた。
方針
/usr/src/linux/arch/arm/mach-pxa/cpufreq-pxa2xx.cの、以下のあたりにそれらしいパラメータをつっこんでみる。
83 /*
84 * PXA255 definitions
85 */
86 /* Use the run mode frequencies for the CPUFREQ_POLICY_PERFORMANCE policy */
87 #define CCLKCFG CCLKCFG_TURBO | CCLKCFG_FCS
88
89 static pxa_freqs_t pxa255_run_freqs[] =
90 {
91 /* CPU MEMBUS CCCR DIV2 CCLKCFG run turbo PXbus SDRAM */
92 { 99500, 99500, 0x121, 1, CCLKCFG, -1, -1}, /* 99, 99, 50, 50 */
93 {132700, 132700, 0x123, 1, CCLKCFG, -1, -1}, /* 133, 133, 66, 66 */
94 {199100, 99500, 0x141, 0, CCLKCFG, -1, -1}, /* 199, 199, 99, 99 */
95 {265400, 132700, 0x143, 1, CCLKCFG, -1, -1}, /* 265, 265, 133, 66 */
96 {331800, 165900, 0x145, 1, CCLKCFG, -1, -1}, /* 331, 331, 166, 83 */
97 {398100, 99500, 0x161, 0, CCLKCFG, -1, -1}, /* 398, 398, 196, 99 */
98 };
パラメータの意味
- CPU : クロック周波数(KHz)
- MEMBUS : メモリーバスクロック周波数(KHz)
- CCCR : Core Clock Configuration Register
- DIV2 : 実際のMEMBUSを2分の1にするかどうかのフラグ(`1' : 2分の1にする)
- run(コメント) : run モード時のクロック周波数(MHz)
- turbo(コメント) : turbo モード時のクロック周波数(MHz)
- PXbus(コメント) : PXbusクロック周波数(MHZ)
- SDRAM(コメント) : メモリーバスクロック周波数(MHz)
CCCR
9,8,7 bit [パラメータ N]
- [0,1,0] = 1倍
- [0,1,1] = 1.5倍
- [1,0,0] = 2倍
- [1,1,0] = 3倍
6,5 bit [パラメータ M]
- [0,1] = 1倍
- [1,0] = 2倍
- [1,1] = 4倍
4,3,2,1,0 bit [パラメータ L]
- [0,0,0,0,1] = 27倍
- [0,0,0,1,0] = 32倍(PXA255のドキュメントでは「予約」とされているが実際には設定可能であるらしい)
- [0,0,0,1,1] = 36倍
- [0,0,1,0,1] = 45倍
で、ようはこのCCCRに適切な値を放りこめば良いようだ...。
基本クロック(Crystal Frequency)
基本クロック(Crystal Frequency)は、3.6864MHz
MEMBUS
MEMBUS = 基本クロック x [パラメータ L]で定めた倍数(MHz)
DIV2
MEMBUSが99.5MHzを越えた場合、1とする。その場合MEMBUSの値は2分の1となる。ただし、実際にはMEMBUSの値は118MHzを越えるまでは2分の1にする必要はなさそう。
run
run = MEMBUS x [パラメータ M]で定めた倍数(MHz)
turbo(=CPU)
turbo = run x [パラメータ N]で定めた倍数(MHz)
CCCR = 0x161 (sharp romのデフォルト設定値)
CCCR = 0x161 = 0,1,0|1,1|0,0,0,0,1
- パラメータ N = 1倍
- パラメータ M = 4倍
- パラメータ L = 27倍
上記のパラメータより
- MEMBUS = 3.6864MHz * 27 = 99.5MHz
- run = 99.5MHz * 4 = 398MHz
- turbo = 398MHz * 1 = 398MHz
- PXbus = 398MHz / 2 = 199MHz
pxa255_run_freqs上での定義は
/* CPU MEMBUS CCCR DIV2 CCLKCFG run turbo PXbus SDRAM */
{398100, 99500, 0x161, 0, CCLKCFG, -1, -1}, /* 398, 398, 196, 99 */
xengineの値は 1290〜1300rpm程度。
オーバークロック時にこの値をどれぐらい上回ることができるのかが勝負どころとなる。
CPU(turbo)が高くても、MEMBUSやPXbusが低いとトータルとしての性能は低くなることが有るので、そのあたりはcut&tryで探る。
CCCR = 0x162
スペシャルカーネル(linux-2.4.x)で、SL-C760やSL-C860のオーバークロック時に採用されている設定。十分に先人達が吟味した値なので、やはりパフォーマンス的には最も高いようだ。
CCCR = 0x162 = 0,1,0|1,1|0,0,0,1,0
- パラメータ N = 1倍
- パラメータ M = 4倍
- パラメータ L = 32倍
上記のパラメータより
- MEMBUS = 3.6864MHz * 32 = 118MHz
- run = 99.5MHz * 4 = 472MHz
- turbo = 472MHz * 1 = 472MHz
- PXbus = 472MHz / 2 = 236MHz
pxa255_run_freqs上での定義は
/* CPU MEMBUS CCCR DIV2 CCLKCFG run turbo PXbus SDRAM */
{471900, 118000, 0x162, 0, CCLKCFG, -1, -1}, /* 472, 472, 236, 118 */
さすがにCPU(turbo)、MEMBUS、PXbusともにバランス良く高い値となっている。
MEMBUS=118MHzは、マニュアル的にいえばDIV2=1として2分の1にするべきなのだが、実際やってみるとDIV2=0で、MEMBUS=118MHzで動くようだ。ただし、マウスポインターがかなり派手にブレまくってかなり使いずらい。
xengineの値は 1380-1390rpm程度。
CCCR = 0x1C5
今回、私が採用した値。マウスポインターのブルブルも許容出来る範囲で、パフォーマンスもそこそこ高いと思う。
CCCR = 0x1C5 = 0,1,1|1,0|0,0,1,0,1
- パラメータ N = 1.5倍
- パラメータ M = 2倍
- パラメータ L = 45倍
上記のパラメータより
- MEMBUS = 3.6864MHz * 45 / 2 = 83MHz
- run = (83MHz * 2) * 2 = 332MHz
- turbo = 332MHz * 1.5 = 498MHz
- PXbus = 332MHz / 2 = 166MHz
pxa255_run_freqs上での定義は
/* CPU MEMBUS CCCR DIV2 CCLKCFG run turbo PXbus SDRAM */
{497700, 165900, 0x1c5, 1, CCLKCFG, -1, -1}, /* 332, 498, 166, 83 */
xengineの値は 1370-1380rpm程度。
2011/09/29 追記:よくよく使ってみると、やはりマウスポインターのブルブルがかなりのもので、許容できる範囲ではなかった...。
一応
/* CPU MEMBUS CCCR DIV2 CCLKCFG run turbo PXbus SDRAM */
{597200, 99500, 0x1e1, 0, CCLKCFG, -1, -1}, /* 398, 597, 199, 99 */
{707800, 118000, 0x1e2, 0, CCLKCFG, -1, -1}, /* 472, 708, 236, 118 */
でも起動できた。ただしxengineの値は CCCR=0x162 がピークで、それを越えることは無かった。
build kernel
- make menuconf
- make; make modules_install
- cp /boot/zImage-2.6.36.4 /boot/zImage-2.6.36.4.old
- cp arch/arm/boot/zImage /boot/zImage-2.6.36.4
- cd /boot
- ln -s zImage-2.6.36.4 zImage
/etc/apmd_proxy
オーバークロックしている状態だと、ほぼ確実にレジュームに失敗するので、サスペンド前にpowersaveモードに遷移させ、レジューム時に元に戻すように設定した。
#! /bin/sh
case $1 in
suspend )
/usr/bin/cp /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor /tmp/scaling_governor.state
/usr/bin/cp /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed /tmp/scaling_setspeed.state
/bin/echo powersave >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
;;
resume )
/usr/bin/cp /tmp/scaling_governor.state /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
/usr/bin/cp /tmp/scaling_setspeed.state /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed
/usr/bin/rm /tmp/scaling_governor.state
/usr/bin/rm /tmp/scaling_setspeed.state
;;
* )
;;
esac
今後の目標
- w100関係のシステムクロックを上げて、描画のパフォーマンスを上げる。
- ads7846関係の同期タイミングを調整してマウスカーソルのブルブルを軽減する。
- runモード、turboモードをいまいち理解していない。zaurusでは常にturboモードなのか?。ちゃんと理解したい。
おまけ
$ cat showclock.rb
#! /usr/bin/ruby
crystal = 3.6864
#
np = ["1", "1 8 +", "2", "3"];
nv = [1, 1.5, 2, 3];
#
mp = ["2", "4", "6"];
mv = [1, 2, 4];
#
lp = ["1", "2", "3", "5"];
lv = [27, 32, 36, 45];
#
li = 0;
while (li < 4)
mi = 0;
while (mi < 3)
ni = 0;
while (ni < 4)
printf("turbo %d run %d membus %d (DIV2=0) %d (DIV2=1) pxbus %d cccr %s %s %s\n",
crystal * lv[li] * mv[mi] * nv[ni],
crystal * lv[li] * mv[mi],
crystal * lv[li], crystal * lv[li] / 2,
crystal * lv[li] * mv[mi] / 2,
np[ni], mp[mi], lp[li]);
ni += 1;
end
mi += 1;
end
li += 1;
end
$ ./showclock.rb |sort -t ' ' -n +1 -2
turbo 99 run 99 membus 99 (DIV2=0) 49 (DIV2=1) pxbus 49 cccr 1 2 1
turbo 117 run 117 membus 117 (DIV2=0) 58 (DIV2=1) pxbus 58 cccr 1 2 2
turbo 132 run 132 membus 132 (DIV2=0) 66 (DIV2=1) pxbus 66 cccr 1 2 3
turbo 149 run 99 membus 99 (DIV2=0) 49 (DIV2=1) pxbus 49 cccr 1 8 + 2 1
turbo 165 run 165 membus 165 (DIV2=0) 82 (DIV2=1) pxbus 82 cccr 1 2 5
turbo 176 run 117 membus 117 (DIV2=0) 58 (DIV2=1) pxbus 58 cccr 1 8 + 2 2
turbo 199 run 132 membus 132 (DIV2=0) 66 (DIV2=1) pxbus 66 cccr 1 8 + 2 3
turbo 199 run 199 membus 99 (DIV2=0) 49 (DIV2=1) pxbus 99 cccr 1 4 1
turbo 199 run 99 membus 99 (DIV2=0) 49 (DIV2=1) pxbus 49 cccr 2 2 1
turbo 235 run 117 membus 117 (DIV2=0) 58 (DIV2=1) pxbus 58 cccr 2 2 2
turbo 235 run 235 membus 117 (DIV2=0) 58 (DIV2=1) pxbus 117 cccr 1 4 2
turbo 248 run 165 membus 165 (DIV2=0) 82 (DIV2=1) pxbus 82 cccr 1 8 + 2 5
turbo 265 run 132 membus 132 (DIV2=0) 66 (DIV2=1) pxbus 66 cccr 2 2 3
turbo 265 run 265 membus 132 (DIV2=0) 66 (DIV2=1) pxbus 132 cccr 1 4 3
turbo 298 run 199 membus 99 (DIV2=0) 49 (DIV2=1) pxbus 99 cccr 1 8 + 4 1
turbo 298 run 99 membus 99 (DIV2=0) 49 (DIV2=1) pxbus 49 cccr 3 2 1
turbo 331 run 165 membus 165 (DIV2=0) 82 (DIV2=1) pxbus 82 cccr 2 2 5
turbo 331 run 331 membus 165 (DIV2=0) 82 (DIV2=1) pxbus 165 cccr 1 4 5
turbo 353 run 117 membus 117 (DIV2=0) 58 (DIV2=1) pxbus 58 cccr 3 2 2
turbo 353 run 235 membus 117 (DIV2=0) 58 (DIV2=1) pxbus 117 cccr 1 8 + 4 2
turbo 398 run 132 membus 132 (DIV2=0) 66 (DIV2=1) pxbus 66 cccr 3 2 3
turbo 398 run 199 membus 99 (DIV2=0) 49 (DIV2=1) pxbus 99 cccr 2 4 1
turbo 398 run 265 membus 132 (DIV2=0) 66 (DIV2=1) pxbus 132 cccr 1 8 + 4 3
turbo 398 run 398 membus 99 (DIV2=0) 49 (DIV2=1) pxbus 199 cccr 1 6 1
turbo 471 run 235 membus 117 (DIV2=0) 58 (DIV2=1) pxbus 117 cccr 2 4 2
turbo 471 run 471 membus 117 (DIV2=0) 58 (DIV2=1) pxbus 235 cccr 1 6 2
turbo 497 run 165 membus 165 (DIV2=0) 82 (DIV2=1) pxbus 82 cccr 3 2 5
turbo 497 run 331 membus 165 (DIV2=0) 82 (DIV2=1) pxbus 165 cccr 1 8 + 4 5
turbo 530 run 265 membus 132 (DIV2=0) 66 (DIV2=1) pxbus 132 cccr 2 4 3
turbo 530 run 530 membus 132 (DIV2=0) 66 (DIV2=1) pxbus 265 cccr 1 6 3
turbo 597 run 199 membus 99 (DIV2=0) 49 (DIV2=1) pxbus 99 cccr 3 4 1
turbo 597 run 398 membus 99 (DIV2=0) 49 (DIV2=1) pxbus 199 cccr 1 8 + 6 1
turbo 663 run 331 membus 165 (DIV2=0) 82 (DIV2=1) pxbus 165 cccr 2 4 5
turbo 663 run 663 membus 165 (DIV2=0) 82 (DIV2=1) pxbus 331 cccr 1 6 5
turbo 707 run 235 membus 117 (DIV2=0) 58 (DIV2=1) pxbus 117 cccr 3 4 2
turbo 707 run 471 membus 117 (DIV2=0) 58 (DIV2=1) pxbus 235 cccr 1 8 + 6 2
turbo 796 run 265 membus 132 (DIV2=0) 66 (DIV2=1) pxbus 132 cccr 3 4 3
turbo 796 run 398 membus 99 (DIV2=0) 49 (DIV2=1) pxbus 199 cccr 2 6 1
turbo 796 run 530 membus 132 (DIV2=0) 66 (DIV2=1) pxbus 265 cccr 1 8 + 6 3
turbo 943 run 471 membus 117 (DIV2=0) 58 (DIV2=1) pxbus 235 cccr 2 6 2
turbo 995 run 331 membus 165 (DIV2=0) 82 (DIV2=1) pxbus 165 cccr 3 4 5
turbo 995 run 663 membus 165 (DIV2=0) 82 (DIV2=1) pxbus 331 cccr 1 8 + 6 5
turbo 1061 run 530 membus 132 (DIV2=0) 66 (DIV2=1) pxbus 265 cccr 2 6 3
turbo 1194 run 398 membus 99 (DIV2=0) 49 (DIV2=1) pxbus 199 cccr 3 6 1
turbo 1327 run 663 membus 165 (DIV2=0) 82 (DIV2=1) pxbus 331 cccr 2 6 5
turbo 1415 run 471 membus 117 (DIV2=0) 58 (DIV2=1) pxbus 235 cccr 3 6 2
turbo 1592 run 530 membus 132 (DIV2=0) 66 (DIV2=1) pxbus 265 cccr 3 6 3
turbo 1990 run 663 membus 165 (DIV2=0) 82 (DIV2=1) pxbus 331 cccr 3 6 5
$
2020年09月22日 C# memo
_ C# memo
HelloWorld.cs
using System;
public class AnyClassYouLike{
static void Main(string[] args){
Console.WriteLine("HelloWorld");
return;
}
}
変数
sbyte //8ビット整数(符号付き) byte //8ビット整数(符号無し) short //16ビット整数(符号付き) ushort //16ビット整数(符号無し) int //32ビット整数(符号付き) uint //32ビット整数(符号無し) long //64ビット整数(符号付き) ulong //64ビット整数(符号無し) float //32ビット浮動小数(例 123.456f) double //64ビット浮動小数(例 123.456d) char //文字(UNICODE)(例 'あ') bool //論理値 (true or false) decimal //高精度 10進数浮動小数(例 123.456m) string //文字列(UNICODE)(例 "あいうえお") object //全ての型の基底となる型
配列
int[] a = new int[3] {1,2,3};
int[,] a = new int[2,3] {{1,2,3},{4,5,6}};
ハッシュ
using System.Collections;
Hashtable ht = new Hashtable();
ht["key1"] = "data1";
// ht.ContainsKey("key1") // True or False
// foreach (string key in ht.Keys){ ... }
標準入出力
using System;
Console.Write("あいうえお"); // 改行無し
Console.WriteLine("あいうえお"); // 改行有り
Console.WriteLine("私は{0}{1}です","伊藤","太郎");
string input = Console.ReadLine();
文字列フォーマット
string str = string.Format("{(インデックス)[,(表示幅)][:(書式)(精度)]}", 値)
// (インデックス) 0オリジン
// ,(表示幅) 整数値、マイナス指定は左寄せ
// :(書式)(精度) d 整数値の10進表記、(精度)は桁数を表す(先頭ゼロ埋め)
// e 指数表記、(精度)は仮数部の小数点桁数を表す
// f 固定小数点表記、(精度)は小数点桁数を表す
// n 3桁区切り文字入りの数値表記、(精度)は小数点桁数を表す
// x 整数値の16進表記、(精度)は桁数を表す(先頭ゼロ埋め)
string str = string.Format("{(インデックス):###,###,###}", 値)
// 可変長、先頭ゼロ埋め無し、桁区切り付き、マイナス符号付き
string str = string.Format("{(インデックス):000,000,000}", 値)
// 固定長、先頭ゼロ埋め有り、桁区切り付き、マイナス符号付き
条件分岐
if (a == 1){
}else if (a == 2){
}else{
}
繰返し
using System;
using System.Collections;
while (a < 10){
}
for (int i = 0; i < 10; i++){
}
int[] array = new int[10] {1,2,3,4,5,6,7,8,9,10};
foreach (int i in array){
}
Hashtable hash = new Hashtable() {
{"key1","value1"},
{"key2","value2"},
{"key3","value3"}
};
ArrayList list = new ArrayList (hash.Keys);
list.Sort();
foreach (string key in list){
}
ファイル入出力(テキスト)
using System.IO;
using System.Text;
StreamReader sr = new StreamReader("input.csv", Encoding.GetEncoding("UTF-8"));
bool append_sw = false;
StreamWriter sw = new StreamWriter("output.csv", append_sw, Encoding.GetEncoding("UTF-8"));
string str = null;
while ((str = sr.ReadLine()) != null){
sw.WriteLine(str);
}
sr.Close();
sw.Close();
ファイル入出力(バイナリ)
using System.IO;
byte[] buf = new byte[1024];
FileStream sr = new FileStream("input.dat", FileMode.Open, FileAccess.Read);
FileStream sw = new FileStream("output.dat", FileMode.Create, FileAccess.Write);
int readsize = 0;
while ((readsize = sr.Read(buf, 0, 1024)) > 0){
sw.Write(buf, 0, readsize);
}
sr.Close();
sw.Close();
ファイル入出力(バイナリ2)
using System.IO;
byte[] buf = new byte[1024];
byte b;
FileStream srw = new FileStream("output.dat", FileMode.Open, FileAccess.ReadWrite);
int readsize = 0;
srw.Seek(10, SeekOrigin.Begin);
readsize = srw.Read(buf, 0, 5);
b = buf[0];buf[0] = buf[4]; buf[4] = b;
b = buf[1];buf[1] = buf[3]; buf[3] = b;
srw.Seek(10, SeekOrigin.Begin);
srw.Write(buf, 0, 5);
srw.Close();
引数
using System;
public class AnyClassYouLike{
static void Main(string[] args){
for (int i = 0; i < args.Length; i++){
Console.WriteLine(args[i]);
}
return;
}
}
文字列連結
string str = "あいう" + "えお";
文字列分割
using System.Text.RegularExpressions; string input = "aa,bb,,cc,,,dd"; string sep = ",+"; string[] array = Regex.Split(input, sep);
部分文字列
string str = "abcあいうdef"; string hiragana = str.Substring(3, 3);
文字列比較(正規表現)
using System.Text.RegularExpressions;
if (Regex.IsMatch("abcdefg", "b.*e")){ ... }
文字列置換(正規表現)
using System.Text.RegularExpressions;
string str = Regex.Replace("abcdefg", "b.*f", "b to f");
string str = Regex.Replace("abcdefg", "^(.)(.*)(.)$", "$3$2$1");
文字列⇒数値変換
int i = int.Parse("123");
float f = float.Parse("123.456");
型変換
int i = (int)123.45;
日時取得
DateTime dt;
dt = new DateTime(2020,9,20,12,59,59);
Console.WriteLine(dt.ToString());
Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm:ss"));
Console.WriteLine(dt.Year);
Console.WriteLine(dt.Month);
Console.WriteLine(dt.Day);
Console.WriteLine(dt.Hour);
Console.WriteLine(dt.Minute);
Console.WriteLine(dt.Second);
dt = DateTime.Parse("2020/09/20 12:59:59");
dt = DateTime.Now;
DateTime dt2 = dt.AddYears(10); // AddMonths(), AddDays(), AddHours(), AddMinutes(), AddSeconds()
Console.WriteLine(dt.CompareTo(dt2)); // -1 (-1, 0, 1)
外部コマンド実行
using System; using System.Diagnostics; ProcessStartInfo pInfo = new ProcessStartInfo(); pInfo.FileName = "ipconfig.exe"; pInfo.CreateNoWindow = true; // do not open console window pInfo.UseShellExecute = false; // false for console command, true for windows app pInfo.RedirectStandardOutput = true; pInfo.RedirectStandardError = true; pInfo.Arguments = "/all"; Process p = Process.Start(pInfo); p.WaitForExit(); String stdOut = p.StandardOutput.ReadToEnd(); String stdErr = p.StandardError.ReadToEnd(); int exitCode = p.ExitCode; p.Close(); Console.WriteLine(stdOut); Console.WriteLine(stdErr);
終了
Environment.Exit(0);
例外処理
try{
throw new Exception("エラーだよ");
}catch(Exception e){
Console.WriteLine(e);
Console.WriteLine(e.Message);
}finally{
Console.WriteLine("finally");
}
クラス
using System;
public class AnyClassYouLike{
static void Main(string[] args){
MyClass mc = new MyClass("あいう", "えお");
mc.show();
MyClass2 mc2 = new MyClass2("あいう", "えお", "かきくけこ");
mc2.show();
return;
}
}
public class MyClass{
string p1;
string p2;
public MyClass(){
this.p1 = "";
this.p2 = "";
}
public MyClass(string p1, string p2){
this.p1 = p1;
this.p2 = p2;
}
public virtual void show(){
Console.WriteLine(this.p1 + "," + this.p2);
}
}
public class MyClass2 : MyClass{
string p3;
public MyClass2(){
}
public MyClass2(string p1, string p2, string p3) : base(p1, p2){
this.p3 = p3;
}
public override void show(){
base.show();
Console.WriteLine(this.p3);
}
}
インターフェース
using System;
public class AnyClassYouLike{
static void Main(string[] args){
Ianimal a;
a = new Dog();
a.talk();
a = new Cat();
a.talk();
return;
}
}
interface Ianimal{
void talk();
}
class Dog : Ianimal{
public void talk(){
Console.WriteLine("わんわん");
}
}
class Cat : Ianimal{
public void talk(){
Console.WriteLine("にゃー");
}
}
Oracle
using System;
using System.Data;
using System.Data.OracleClient;
public class AnyClassYouLike{
static void Main(string[] args){
string ip = "127.0.0.1";
string user = "mito";
string password = "oracle";
string connectionString = "Data Source=" + ip + ";User ID=" + user + ";Password=" + password + ";Integrated Security=false;";
OracleConnection connection;
OracleCommand ocmd;
OracleParameter bind_id;
OracleParameter bind_name;
OracleDataReader ord;
int row;
//
connection = new OracleConnection(connectionString);
connection.Open();
ocmd = new OracleCommand();
ocmd.Connection = connection;
ocmd.Transaction = connection.BeginTransaction();
//
ocmd.CommandText = "insert into t_sample (id,name) values(:id,:name)";
bind_id = new OracleParameter("id", "00000001");
ocmd.Parameters.Add(bind_id);
bind_name = new OracleParameter("name", "伊藤 太郎");
ocmd.Parameters.Add(bind_name);
row = ocmd.ExecuteNonQuery();
ocmd.Parameters.Remove(bind_id);
ocmd.Parameters.Remove(bind_name);
ocmd.Transaction.Commit(); // or .Rollback();
ocmd.Transaction = connection.BeginTransaction();
//
ocmd.CommandText = "select id,name from t_sample where id >= :id";
bind_id = new OracleParameter("id", "00000000");
ocmd.Parameters.Add(bind_id);
ord = ocmd.ExecuteReader();
while (ord.Read()){
Console.WriteLine(ord["id"] + "," + ord["name"]);
}
ord.Close();
ocmd.Parameters.Remove(bind_id);
//
ocmd.CommandText = "update t_sample set name=:name where id = :id";
bind_id = new OracleParameter("id", "00000001");
ocmd.Parameters.Add(bind_id);
bind_name = new OracleParameter("name", "伊藤 花子");
ocmd.Parameters.Add(bind_name);
row = ocmd.ExecuteNonQuery();
ocmd.Parameters.Remove(bind_id);
ocmd.Parameters.Remove(bind_name);
ocmd.Transaction.Commit(); // or .Rollback();
ocmd.Transaction = connection.BeginTransaction();
//
ocmd.CommandText = "select id,name from t_sample where id >= :id";
bind_id = new OracleParameter("id", "00000000");
ocmd.Parameters.Add(bind_id);
ord = ocmd.ExecuteReader();
while (ord.Read()){
Console.WriteLine(ord["id"] + "," + ord["name"]);
}
ord.Close();
ocmd.Parameters.Remove(bind_id);
//
ocmd.CommandText = "delete from t_sample where id >= :id";
bind_id = new OracleParameter("id", "00000000");
ocmd.Parameters.Add(bind_id);
row = ocmd.ExecuteNonQuery();
ocmd.Parameters.Remove(bind_id);
ocmd.Transaction.Commit(); // or .Rollback();
//
ord.Dispose();
ocmd.Dispose();
connection.Close();
connection.Dispose();
return;
}
}
MSSQLServer
using System;
using System.Data;
using System.Data.SqlClient;
public class AnyClassYouLike{
static void Main(string[] args){
SqlConnectionStringBuilder builder;
SqlConnection con;
SqlCommand com;
SqlParameter parm_id;
SqlParameter parm_name;
SqlDataReader reader;
SqlTransaction transaction;
string sqlstr;
builder = new SqlConnectionStringBuilder();
builder.DataSource = "127.0.0.1";
builder.UserID = "mito";
builder.Password = "sqlserver";
builder.InitialCatalog = "testdb";
builder.ConnectTimeout = 15000; // ms
con = new SqlConnection(builder.ConnectionString);
con.Open();
//
sqlstr = "insert into t_sample (id,name) values(@id,@name)";
com = new SqlCommand(sqlstr, con);
com.CommandText = sqlstr;
transaction = con.BeginTransaction();
com.Transaction = transaction;
parm_id = new SqlParameter("@id", "00000001");
com.Parameters.Add(parm_id);
parm_name = new SqlParameter("@name", "伊藤 太郎");
com.Parameters.Add(parm_name);
com.ExecuteNonQuery();
com.Parameters.Remove(parm_id);
com.Parameters.Remove(parm_name);
com.Dispose();
transaction.Commit(); // or .Rollback();
//
sqlstr = "select id,name from t_sample where id >= @id";
com = new SqlCommand(sqlstr, con);
com.CommandText = sqlstr;
transaction = con.BeginTransaction();
com.Transaction = transaction;
parm_id = new SqlParameter("@id", "00000000");
com.Parameters.Add(parm_id);
reader = com.ExecuteReader();
while (reader.Read()){
Console.WriteLine(reader["id"] + "," + reader["name"]);
}
reader.Close();
com.Parameters.Remove(parm_id);
com.Dispose();
transaction.Commit(); // or .Rollback();
//
sqlstr = "update t_sample set name=@name where id = @id";
com = new SqlCommand(sqlstr, con);
com.CommandText = sqlstr;
transaction = con.BeginTransaction();
com.Transaction = transaction;
parm_id = new SqlParameter("@id", "00000001");
com.Parameters.Add(parm_id);
parm_name = new SqlParameter("@name", "伊藤 花子");
com.Parameters.Add(parm_name);
com.ExecuteNonQuery();
com.Parameters.Remove(parm_id);
com.Parameters.Remove(parm_name);
com.Dispose();
transaction.Commit(); // or .Rollback();
//
sqlstr = "select id,name from t_sample where id >= @id";
com = new SqlCommand(sqlstr, con);
com.CommandText = sqlstr;
transaction = con.BeginTransaction();
com.Transaction = transaction;
parm_id = new SqlParameter("@id", "00000000");
com.Parameters.Add(parm_id);
reader = com.ExecuteReader();
while (reader.Read()){
Console.WriteLine(reader["id"] + "," + reader["name"]);
}
reader.Close();
com.Parameters.Remove(parm_id);
com.Dispose();
transaction.Commit(); // or .Rollback();
//
sqlstr = "delete from t_sample where id >= @id";
com = new SqlCommand(sqlstr, con);
com.CommandText = sqlstr;
transaction = con.BeginTransaction();
com.Transaction = transaction;
parm_id = new SqlParameter("@id", "00000000");
com.Parameters.Add(parm_id);
com.ExecuteNonQuery();
com.Parameters.Remove(parm_id);
com.Dispose();
transaction.Commit(); // or .Rollback();
//
con.Close();
return;
}
}
Socket通信(echo server (single thread))
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text.RegularExpressions;
public class AnyClassYouLike{
static void Main(String[] args){
string line;
IPAddress ipAdd = IPAddress.Parse("0.0.0.0");
int port = 1234;
TcpListener listener = new TcpListener(ipAdd, port);
listener.Start();
while (true){
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("クライアント({0}:{1})と接続しました。",
((IPEndPoint)client.Client.RemoteEndPoint).Address,
((IPEndPoint)client.Client.RemoteEndPoint).Port);
NetworkStream ns = client.GetStream();
StreamReader sr = new StreamReader(ns, Encoding.UTF8);
StreamWriter sw = new StreamWriter(ns, Encoding.UTF8);
while ((line = sr.ReadLine()) != null){
Console.WriteLine(line);
sw.WriteLine(line);
sw.Flush();
if (Regex.IsMatch(line, "^shutdown")){
break;
}
}
sr.Close();
sw.Close();
ns.Close();
client.Close();
if (line != null){
break;
}
}
listener.Stop();
}
}
Socket通信(echo client)
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
public class AnyClassYouLike{
static void Main(String[] args){
string line;
string ipAdd = args[0];
int port = int.Parse(args[1]);
string str = args[2];
TcpClient client = new TcpClient(ipAdd, port);
Console.WriteLine("クライアント({0}:{1})と接続しました。",
((IPEndPoint)client.Client.RemoteEndPoint).Address,
((IPEndPoint)client.Client.RemoteEndPoint).Port);
NetworkStream ns = client.GetStream();
StreamReader sr = new StreamReader(ns, Encoding.UTF8);
StreamWriter sw = new StreamWriter(ns, Encoding.UTF8);
sw.WriteLine(str);
sw.Flush();
line = sr.ReadLine();
Console.WriteLine(line);
sr.Close();
sw.Close();
ns.Close();
client.Close();
}
}
Socket通信(echo server (multi thread))
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
public class AnyClassYouLike{
static void Main(String[] args){
IPAddress ipAdd = IPAddress.Parse("0.0.0.0");
int port = 1234;
TcpListener listener = new TcpListener(ipAdd, port);
listener.Start();
while (true){
TcpClient client = listener.AcceptTcpClient();
Task.Run(() => communicateClient(client));
Console.WriteLine("クライアント({0}:{1})と接続しました。",
((IPEndPoint)client.Client.RemoteEndPoint).Address,
((IPEndPoint)client.Client.RemoteEndPoint).Port);
}
listener.Stop();
}
private static void communicateClient(TcpClient client){
string line;
NetworkStream ns = client.GetStream();
StreamReader sr = new StreamReader(ns, Encoding.UTF8);
StreamWriter sw = new StreamWriter(ns, Encoding.UTF8);
while ((line = sr.ReadLine()) != null){
Console.WriteLine(line);
sw.WriteLine(line);
sw.Flush();
}
sr.Close();
sw.Close();
ns.Close();
client.Close();
}
}